Sign in to follow this  
unholyx

Nub question about srand/rand

Recommended Posts

unholyx    102
I'm working on a simple blackjack program and I got stuck at a point in generating random numbers.

First I did something like this:

num = rand() % 10 + 1;

only problem was every time i re-executed, it gave me the same number. So I did some more googling on why it did that, and it was because I wasn't putting srand(time(NULL)); anywhere in my code.

Now I'm confused as to what srand does. The description on most sites was that it generated a "seed" value which I don't know what that means, and I also moved srand(time(NULL)); from function to function and eventually to main and whenever rand() was called, it generated a different number every time.

So it seems that as long as I call srand(time(NULL)); -- doesn't matter where -- in my program the number will still generate randomly from rand()?

My program is working, I just need some more clarification on srand and rand and whether if I understood it right.

Thanks.

Share this post


Link to post
Share on other sites
zacaj    667
Basically, rand doesnt actually generate a random number. Basically, rand() generates a new number based on the last number generated, so each time you call rand() you get a new number, but the first time you call rand(), it always uses the same number, so you always get the same set of numbers. srand changes that last number to whatever you give it as an argument. WHen you call srand(time), it takes the current time(which is obviously unique), and uses it for the next rand() call, so you wont ever get the same sequence of numbers. You only ever need to call srand() once per program execution.

Share this post


Link to post
Share on other sites
unholyx    102
[quote name='zacaj' timestamp='1313462169' post='4849673']
Basically, rand doesnt actually generate a random number. Basically, rand() generates a new number based on the last number generated, so each time you call rand() you get a new number, but the first time you call rand(), it always uses the same number, so you always get the same set of numbers. srand changes that last number to whatever you give it as an argument. WHen you call srand(time), it takes the current time(which is obviously unique), and uses it for the next rand() call, so you wont ever get the same sequence of numbers. You only ever need to call srand() once per program execution.
[/quote]

Ah ok thanks thanks, that definitely made the picture clearer.

Share this post


Link to post
Share on other sites
rip-off    10979
Note that using a fixed value for rand() can be helpful if you want to debug something, or you want to deterministic unit testing. In lock-step multiplayer games the peers want to start with a known random seed too (in fact de-sync bugs in lock-step games are often caused by incorrectly handling random values).

Share this post


Link to post
Share on other sites
unholyx    102
[quote name='rip-off' timestamp='1313483199' post='4849749']
Note that using a fixed value for rand() can be helpful if you want to debug something, or you want to deterministic unit testing. In lock-step multiplayer games the peers want to start with a known random seed too (in fact de-sync bugs in lock-step games are often caused by incorrectly handling random values).
[/quote]

Could you explain a bit more about lock-step? I didn't really get what it means when I searched on other sites.

Share this post


Link to post
Share on other sites
rip-off    10979
It isn't important, it is just one example where fixing the seed can be very useful. Imagine you want to write a turn based multi-player game, vaguely like civilisation. It involves some form of combat and this is resolved using random numbers.

You can choose to implement the game with no central server. To enable this, each player sends their input to all other players. To resolve combat, each player must generate the same sequence of random numbers. To make this work, before the game starts one of the clients chooses a seed value (perhaps based on its local clock) and all the peers agree to use this value.

The reason it is called "lock step" is that every client needs to wait for the others before it executes the logic for a given "step" (a turn in this example).

Share this post


Link to post
Share on other sites
zacaj    667
Beware, however, that different compilers may implement rand() differently, so if you compiled one with MSVC and another with gcc, you probably wont get the same numbers

Share this post


Link to post
Share on other sites
rip-off    10979
Yes if you actually wanted to write such a game you'd probably grab a particular implementation of a pseudo random generator, particularly so you can have one "lockstep" instance for the synchronised gamestate and can still have other instances for any random values you want to use during animation, etc.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this