Random Numbers ?
Hope this is the right place to post this, it''s a bit of a "newbie" question (not done a huge amount with C/C++, but learning quicker now thanks to GameDev !)
When using the rand() function in C/C++ code (MSVC 6), I always get the same result e.g. some initialisation code, such as:
x = rand() % 10 // Random position 0 to 9
will **always** give me the same sequence of numbers (every time !)
When looking for a true random number generator, I hit a ''brick wall'' of pure mathematics(!). I understand that debates may rage forever as to whether or not a sequence of numbers may be truely random.
But can anyone suggest a simple, succinct (and, as we''re writing games here, quick) method to generate a sequence of psuedo random numbers - (say) integers from 1 to 10 ?
Cheers !eddSoftwww.eddsoft.com
Did you try searching for a previously given answer to this question? I''m pretty sure you''d find one. You don''t happen to be calling srand anywhere, do you? Try putting srand(time(NULL)) at the beginning of your program (you must include time.h).


The rand function needs to be seeded, hence the call(s) to srand. If you seed the rand function with the same number it will give you the same string of random numbers. This string should not repeat itself for I think something like 2^48 numbers or something like that. If you don''t seed the rand func then the it uses a default seed which is of course not random hence giving you the same initial string of ''random'' no.s.
Null and Void''s use of the the clock time to seed the rand function is a good way to generate a different seed each time the rand func is initialised.
The other point is also right if you seeded the rand func with the same number each time you used it you''d have lots of the same number.
Hope this helps.
Null and Void''s use of the the clock time to seed the rand function is a good way to generate a different seed each time the rand func is initialised.
The other point is also right if you seeded the rand func with the same number each time you used it you''d have lots of the same number.
Hope this helps.
A tiny useless piece of trivia: Some people who are really picky XOR the time by the process ID to ensure that multiple instances of the program launched within the same second have different sequences.
Also note that the fact that because rand() produces the same deterministic set of numbers, you can use this to your advantage to compress the results of a random process into a single integer. For example if you have a game like diablo that generates the entire world randomly, and a new player joins the game, you don''t have to pass them the entire set of level data, just pass the seed that was made to generate the level. You will of course have to pass the updated state information since the world was created.
quote:
Original post by invective
Also note that the fact that because rand() produces the same deterministic set of numbers, you can use this to your advantage to compress the results of a random process into a single integer. For example if you have a game like diablo that generates the entire world randomly, and a new player joins the game, you don''t have to pass them the entire set of level data, just pass the seed that was made to generate the level. You will of course have to pass the updated state information since the world was created.
However, if you''re doing this I highly suggest you use a custom built PRNG. The C runtime used by each OS, compiler, or whatever can (and often does) differ, causing odd results if you assume they''re the same.

Thank you for the information guys - I''m picking this up bit-by-bit following Teej''s online ''Tetris'' tutorial and this was bugging me. I''ll try seeding in my game init area and see what happens.
BTW - the info from Invective on how to use the info on seeding to cut down on info sharing was very interesting indeed.
Cheers all
BTW - the info from Invective on how to use the info on seeding to cut down on info sharing was very interesting indeed.
Cheers all
Cheers !eddSoftwww.eddsoft.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement