🔒 Locked
rand()
Advertisement
Original Post
Remember to only use srand() once... Otherwise it may seed with the same time.
show us some code that reproduces the problem
You can get that by setting up your fuction to round to an int at some inopportune time, for example.
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
RIPPL Sports - NFL Statistical Analysis and Prediction System
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
RIPPL Sports - NFL Statistical Analysis and Prediction System
srand( time ( 0 ) );
int randomnumber = rand();
[edited by - MrPoopypants on January 14, 2003 5:57:54 PM]
rand() % rand() ???
That''s surely a 1 in RAND_MAX chance of getting a divide by zero exception?
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
That''s surely a 1 in RAND_MAX chance of getting a divide by zero exception?
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Ed C: If you''ve fixed the problem, let us know please.
These are all bad examples:
srand(rand()%rand())
srand( GetTickCount() * rand() );
srand( GetTickCount() );
Because you arent providing good entropy across the bits of the seed. See the below text which was copied from my post on randomize() in the the Game Programming area. (and as others have pointed out, only set the seed once in your program)
Also keep in mind that srand(time(0)) isn''t good practice for a lot of applications. This typically results in security failures and allows cheating: a number isn''t random if someone can guess it.
For example a lot of online card games have this fault, as the player can guess the seed, and then they know the exact stream of ''random'' cards that will be dealt out.
And if you need better random numbers then at least use a seed value that has a high level of entropy in the bits. Typically people will take a bunch of sources (time + contents of hard disk + ping time to some server + latency of user input + etc) concatenate them all together into a binary blob then run a hashing algorithm on it (i.e. MD5) which mathematically produces an output where all the bits have an equal chance of being set. This can then be used for a seed.
srand(rand()%rand())
srand( GetTickCount() * rand() );
srand( GetTickCount() );
Because you arent providing good entropy across the bits of the seed. See the below text which was copied from my post on randomize() in the the Game Programming area. (and as others have pointed out, only set the seed once in your program)
Also keep in mind that srand(time(0)) isn''t good practice for a lot of applications. This typically results in security failures and allows cheating: a number isn''t random if someone can guess it.
For example a lot of online card games have this fault, as the player can guess the seed, and then they know the exact stream of ''random'' cards that will be dealt out.
And if you need better random numbers then at least use a seed value that has a high level of entropy in the bits. Typically people will take a bunch of sources (time + contents of hard disk + ping time to some server + latency of user input + etc) concatenate them all together into a binary blob then run a hashing algorithm on it (i.e. MD5) which mathematically produces an output where all the bits have an equal chance of being set. This can then be used for a seed.
time may be predictable, but only if you can calculate the number of seconds since midnight, January 1, 1970. Maybe there are a few eccentrics out there that would do that just to cheat. Then you have to work with the clock, try to figure out what the function actually returned by the time that statement was executed... time works for most purposes.
what was the problem?
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement