tags:

views:

111

answers:

5

I quite like being able to generate the same set of pseudo-random data repeatedly, especially with tweaking experimental code. Through observation I would say that rand() seems to give the same sequence of numbers each time*.

Is it guaranteed to do this for repeated executions on the same machine / for different machines / for different architectures?

*For the same seed obviously.

+13  A: 
KennyTM
standard WIN...
Matt Joiner
OK. I agree that for a particular runtime lib the sequence will be the same. So once an application is built (against a specific runtime version) it will always generate the same sequence. But does this extent to different version of the runtime (ie across OS/architecture/runtime versions) etc. If it does that would imply that the standard defines an exact implementation for the rand() algorithm (otherwise how would two independent OS make sure they conform).
Martin York
It doesn't extend across different versions of the runtimes - and if you dynamically link to the implementation of `rand()` it could potentially act differently without rebuilding anything.
Joe Gauterin
@Martin: *But does this extent to different version of the runtime* — no. Actually, it depends on the runtime whether an update will change the behavior.
KennyTM
Thanks for your answer! The tweaking stage only happens on one machine (I added the extended questions for completeness).
Joe
The language in the Standard only guarantees that you'll get the same sequence every time you call `srand()` with the same value *during one execution of the program* (the *"If `srand` is then called..."* implies that it happens within the same program). It would be conforming for an implementation to pick a "master seed" at program startup, which perturbs the algorithm different each time the program is run.
caf
Thank you caf, I have upvoted your answer!
Joe
+1  A: 

No.

The C standard says:

If srand is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated.

But nowhere does it say what the sequence of pseudo-random numbers actually is - so it differs across implementations.

The only guarantee made is that rand() will give the same sequence of numbers for a given seed for a given implementation. There's no guarantee that the sequence will be the same across different machines or different architectures - and it almost certainly won't be.

Joe Gauterin
A: 

If you need to use the exact same set of pseudo-random numbers for experimental purposes, one thing you could do is to use srand to generate a long sequence of random numbers and write them to a file/database. Then, write a portable "random number generator" function that returns values sequentially from that file. That way, you can be assured that you are using the same input data regardless of the platform, srand implementation, or seed value.

bta
Nice idea. If I had a requirement to do that, I would certainly take that approach. My question was borne more out of curiosity than the need for a known sequence.
Joe
A: 

When switching to a different machine/runtime/whatever you might be out of luck. There is another possible choice the drand48 family of functions. These are normalized to use the same algorithm on all machines.

Jens Gustedt
+1  A: 

It is guaranteed to give the same sequence for the same seed passed to srand() - but only for the duration of a single execution of the program. In general, if an implementation has a choice in behaviour, there is no specific requirement for that choice to remain the same across subsequent executions.

It would be conforming for an implementation to pick a "master seed" at each program startup, and use that to perturb the pseudo-random number generator in a way that is different each time the program starts.

If you wish for more determinism, you should implement a PRNG with specific parameters in your program.

caf