Teen Programmers Unite  
 

 

Return to forum top

Pseudo-random numbers (C++)

Posted by deejay [send private reply] at January 17, 2002, 04:54:21 PM

I feel awful stupid asking this, but how do you get random numbers in C++? I thought it was srand(); but I guess I'm not #including the right header file. Can anyone help me out? Please email me at deejay@stories.com; thanks.

Posted by gian [send private reply] at January 17, 2002, 05:12:38 PM

Unfotunately, random numbers are very hard to get... I believe that "rand" returns a random number between 0 and RAND_MAX (or RANDMAX, I don't remember which... look it up) ... so r (your random number) can be found like:

r = rand()/RAND_MAX*10

That *should* return a random number between 0 and 10...

Of course, then again I could be wrong... we shall wait for the judgement of someone wiser than I...

Posted by Psion [send private reply] at January 17, 2002, 07:23:54 PM

The header file you want to include is stdlib.h.

Posted by CodeRed [send private reply] at January 18, 2002, 03:15:28 AM

Here's a tip, seed the random number generator with the system time

Posted by CViper [send private reply] at January 18, 2002, 01:44:11 PM

it think theres a easier way to get a random number between 0 and 10:

rand()%11

gian's way needs float's, since rand()/RAND_MAX is in the range 0...1

Posted by gian [send private reply] at January 18, 2002, 03:16:48 PM

Cviper, both "Numerical recipes in C" and the rand man page say that you should use my method, and they expressely say not to do the modulus one... don't ask me why, although I imagine there is a good reason, I just don't know it :-)

Posted by miken [send private reply] at January 18, 2002, 08:21:09 PM

And speaking of which, Gamedev had one of the best math and physics threads i've ever read, and gian's reading knowledge is very thoroughly tested and explained:

http://gamedev.net/community/forums/topic.asp?topic_id=72305

Enjoy!

Posted by CViper [send private reply] at January 19, 2002, 05:36:51 AM

funny, since i always used %, and i've even seen it in books (GameProgrammingGems i think)

well anyway to make gians way work, you'll have to use floats, otherwise there are just 2 possible values (0 or 10) because of rounding.

eg r = (int)((float)rand()/(float)RAND_MAX)*10

or something like that

i actually tested it (10000*10000 rand()'s), and got only 2800 times not 0, and in those cases it was 10 :)

Posted by Psion [send private reply] at January 19, 2002, 08:57:03 AM

Thanks to implicit casts, you need only do

int r = (double)rand()/RAND_MAX*YOUR_OWN_MAX

Posted by SantSys [send private reply] at January 25, 2002, 12:28:55 PM

srand is used for initiating the starting point for a random number.

void srand( unsigned int seed );

The srand function sets the starting point for generating a series of pseudorandom integers. To reinitialize the generator, use 1 as the seed argument. Any other value for seed sets the generator to a random starting point. rand retrieves the pseudorandom numbers that are generated. Calling rand before any call to srand generates the same sequence as calling srand with seed passed as 1.

rand retrieves the psudo random number.

int rand( void );

The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. Use the srand function to seed the pseudorandom-number generator before calling rand.

(info from MSDN - January 2000)

Posted by CodeRed [send private reply] at January 27, 2002, 04:02:30 PM

Use this to seed:

time_t t;
srand((unsigned) time(&t));

Make sure to include time.h

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.