By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,494 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,494 IT Pros & Developers. It's quick & easy.

To generate a random numbers

P: n/a
I am doing a project where I want to generate random numbers for say n
people....
These numbers should be unique for n people.

Two people should not have same numbers....

Also the numbers should not be repeted..

Do anyone have some nice algorithm for that? Or anyone can suggest me
any type of book or site for that purpose?

Thaks for answer....

Aug 12 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Anamika" <sh*************@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com
I am doing a project where I want to generate random numbers for say n
people....
These numbers should be unique for n people.

Two people should not have same numbers....

Also the numbers should not be repeted..

Do anyone have some nice algorithm for that? Or anyone can suggest me
any type of book or site for that purpose?

Thaks for answer....
Just create an array with non-random numbers and then call the
random_shuffle function from the C++ standard library (declared in the
<algorithmheader).

--
John Carson
Aug 12 '06 #2

P: n/a
"Anamika" <sh*************@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>I am doing a project where I want to generate random numbers for say n
people....
These numbers should be unique for n people.

Two people should not have same numbers....

Also the numbers should not be repeted..

Do anyone have some nice algorithm for that? Or anyone can suggest me
any type of book or site for that purpose?

Thaks for answer....
It depends on your range of random numbers. If it's rather arbitrary
(0-65535 or something) then I think the simplest way would to create a
random number using rand, then look and see if it's been used already. If
it has, then generate another one. For small values of n this shouldn't be
a problem. If you're trying to generate a random number for everyone in the
united states however it might be a bit time consuming.
Aug 12 '06 #3

P: n/a
Do anyone have some nice algorithm for that? Or anyone can suggest me
any type of book or site for that purpose?
Using the built-in C/C++ facilities for random number generation is
almost certainly a terrible idea. The rand() method is totally
inadequate for a large number of users and purposes.

I'd suggest using your operating system's built-in facilities for
random number generation. On Windows, check MSDN
(http://msdn.microsoft.com) for CryptAcquireContext and CryptGenRandom.
On UNIX, check to see if /dev/urandom will meet your needs.

This is an area that is far, far outside the realm of the standard C++
language. I'd suggest that you try reposting this message in a
newsgroup appropriate for your operating system; they'll be able to
give you much more help than we can.

Aug 12 '06 #4

P: n/a
Anamika wrote:
I am doing a project where I want to generate random numbers for say n
people....
These numbers should be unique for n people.

Two people should not have same numbers....

Also the numbers should not be repeted..
y =y * 2147001325L + 715136305L;

initialize the y (which, obviously, is a 32-bit unsigned long) with any
number in range 0 to 2^32-1, then it generates 2^32 unique numbers from 0 to
0xffffffff.
After 2^32 numbers are generated, the function will repeat exactly the same
sequence.

Very important: the numbers are *not* random, but just pseudo-random.

Cristiano
Aug 12 '06 #5

P: n/a
Anamika wrote:
I am doing a project where I want to generate random numbers for say n
people....
These numbers should be unique for n people.

Two people should not have same numbers....

Also the numbers should not be repeted..

Do anyone have some nice algorithm for that? Or anyone can suggest me
any type of book or site for that purpose?

Thaks for answer....
Set a counter (n = 1)

To assign a "random" number

person[i] = n++;

The randomness facter will be low. You can hide the
fact somewhat by swapping bits in the generated number.

x = (n & 1 << 3) | (n & 2 >1) | (n & 3 >1) <etc...>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Aug 14 '06 #6

P: n/a
"Anamika" <sh*************@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>I am doing a project where I want to generate random numbers for say n
people....
These numbers should be unique for n people.

Two people should not have same numbers....

Also the numbers should not be repeted..

Do anyone have some nice algorithm for that? Or anyone can suggest me
any type of book or site for that purpose?

Thaks for answer....
I just though of another way. Use std::set.

(pseudo code)

std::set<intMyRandoms;
while (MyRandoms.size() < n )
MyRandoms.insert( randomfunction( MaxValue ) );
Aug 15 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.