Teen Programmers Unite  


Return to forum top

Random numbers in C++

Posted by jay_dee [send private reply] at May 27, 2002, 02:52:52 AM

Does anyone know how to make random numbers in c++? My book doesn't cover it but i tryed the c command

var = (rnd())

or something like that but it just kept giveing the same number over and over. thanks,

Posted by slipknotclown [send private reply] at May 27, 2002, 04:01:06 AM

ok theres a problem with rand basically which is what you said, the numbers are the same each time you compile, for more random numbers you can seed the randomiser using these commands.

ok you need <time.h> & <stdlib.h>

srand((unsigned)time(NULL)); I'm afraid I cant realy explain how it works,

Posted by RedX [send private reply] at May 27, 2002, 04:43:42 AM

I believe it usses an array of random numbers and everytime you use rnd() it returns the next value from that list. Using the time as the seed makes rnd() start at a different position in the list every time the program starts.


Posted by CViper [send private reply] at May 27, 2002, 08:41:57 AM

nah... it isn't stored in a list. Each time you take rand() it takes the previously stored seed (or the one set by srand()), does some stuff with it (most implementations basicly do some multiplication with prime numbers, some addition etc), stores the new value as the new seed and then returns it.

Posted by CodeRed [send private reply] at May 27, 2002, 09:32:37 AM

try this, it seeds the random number generator with the system time:

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

Just call seed() before you use rand()
Posted by CodeRed [send private reply] at May 27, 2002, 09:33:50 AM

Oh, you need time.h to use time_t and time()

Posted by Psion [send private reply] at May 27, 2002, 11:21:36 AM

slipknotclown's code was already correct, though you can abbreivate it with srand(time(0)).

Posted by CodeRed [send private reply] at May 27, 2002, 11:26:59 AM

Well, he said he can't explain how it works, which I did, and I put it into a funtion so it is easy to use everytime you need it.

Posted by unknown_lamer [send private reply] at May 27, 2002, 01:06:02 PM

rand is supposed to return the same sequence of numbers every time it is seeded with the same number so that you can easily test your program. When you call srand with the time you are giving it a different seed every time it is run (on a POSIX system this number should never be the same twice until the clock rolls over...but by then time should return a 64-bit number). Note that rand() is not cryptographically secure..for that you should fopen /dev/random (at least on a GNU/Linux or Hurd machine) and then read random numbers from there. You can do the same for /dev/urandom (which returns generated numbers when the entropy pool runs out...these aren't as secure). Basically /dev/[u]random is the user space interface to the entropy pool which is a pool of randomly generated numbers (the numbers are generated by sampling stuff like the time between keypresses, the distance and direction of mouse movements, various interrupts, etc). /dev/random will wait until there is more entropy available to return more random data when it runs out, whereas urandom will just generate more numbers when you run out of entropy. Programs like gnupg use /dev/random (which is why it tells you to type stuff while it generates the key so it doesn't run out of random data). [end random number rant]

Posted by CViper [send private reply] at May 27, 2002, 04:05:48 PM

thats why it's called pseudo random number generator :) And sometimes it's really nice to get the same sequence of the numbers (assuming you use the same seed).

Btw: there's a nice article on generating random numbers [real ones] in GameProgrammingGems 2. I think they use Userinput, HD/other drive buffers, some fancy asm instructions (something about thermal noise) etc.

Posted by jay_dee [send private reply] at May 27, 2002, 04:18:04 PM

alright..thanks i got it worked out,

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

Log on
Save for later automatic logon

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