Skip to main content
gamedev.net
🔒 Locked

rand()

Started by Ed C 14 replies 400+ views
Original Post
Ed C
Ed C
In C++, when trying to rand() twice to make 2 random numbers they allways come out the same, unless there is a big bit of code between the two. It even happens if i include a second ''srand''. Why is this happening and how can i stop it?
Ed C
Ed C
PS: i''m using the system time as the seed in ''srand''.
The Heretic
The Heretic
Remember to only use srand() once... Otherwise it may seed with the same time.
Ed C
Ed C
Even when i only use srand once, i still get the same number twice.
Qw3r7yU10p!
Qw3r7yU10p!
show us some code that reproduces the problem
IADaveMark
IADaveMark
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 of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit<
MrPoopypants
MrPoopypants

srand( time ( 0 ) );

int randomnumber = rand();

[edited by - MrPoopypants on January 14, 2003 5:57:54 PM]
(0110101101000110)The Murphy Philosophy: Smile . . . tomorrow will be worse.
Paradigm Shifter
Paradigm Shifter
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
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Qw3r7yU10p!
Qw3r7yU10p!
Ed C: If you''ve fixed the problem, let us know please.
H4ck3r
H4ck3r
U could use:

srand( GetTickCount() * rand() );
i = rand();

HTH
pawn69
pawn69
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.
Zipster
Zipster
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.
Ed C
Ed C
I think i''ve fixed the problem now.

Thanks guys.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.