Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > To generate a random numbers

Reply
Thread Tools

To generate a random numbers

 
 
Anamika
Guest
Posts: n/a
 
      08-12-2006
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....

 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      08-12-2006
"Anamika" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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
<algorithm> header).

--
John Carson


 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      08-12-2006
"Anamika" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
Robert J. Hansen
Guest
Posts: n/a
 
      08-12-2006
> 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.

 
Reply With Quote
 
Cristiano
Guest
Posts: n/a
 
      08-12-2006
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


 
Reply With Quote
 
Kevin Handy
Guest
Posts: n/a
 
      08-14-2006
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 =----
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      08-15-2006
"Anamika" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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<int> MyRandoms;
while (MyRandoms.size() < n )
MyRandoms.insert( randomfunction( MaxValue ) );


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Re: Generate a sequence of random numbers that sum up to 1? Anthony Liu Python 1 04-29-2006 11:36 PM
Generate a sequence of random numbers that sum up to 1? Anthony Liu Python 14 04-24-2006 06:35 AM
reading internet data to generate random numbers. Levi Campbell Python 20 11-28-2005 03:25 AM



Advertisments