To generate a random numbers

A

Anamika

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....
 
J

John Carson

Anamika said:
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
<algorithm> header).
 
J

Jim Langston

Anamika said:
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.
 
R

Robert J. Hansen

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.
 
C

Cristiano

Anamika said:
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
 
K

Kevin Handy

Anamika said:
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 = 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...>
 
J

Jim Langston

Anamika said:
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<int> MyRandoms;
while (MyRandoms.size() < n )
MyRandoms.insert( randomfunction( MaxValue ) );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top