3

I'm not quite sure how to phrase this question, but I couldn't find any others like it.

Say I have this code:

srand(1);
srand(SOME_DEFINED_CONST_INT);

If I run this executable on a number of different physical machines, is the sequence of rand() guaranteed to be consistent between them? i.e. if I get 1, 4, 6, 3, 4 on one machine, will I always get that same sequence on the others?

If yes, how can that be proven? Is it part of the standard?

If no, is there anything I could do to make it so?

badgerr
  • 7,802
  • 2
  • 28
  • 43

4 Answers4

3

No, the standard guarantees no such thing. However, the logic of generating the random numbers is inside the C standard library. So if you build the application with the same version of the library, the sequence should be the same. The second part of my answer is just a guess, but the standard definitely doesn't give any guarantees.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
3

As Armen said, it's non standard. However, if you look at the man page for srand() on Linux, you'll see something interesting:

POSIX 1003.1-2003 gives the following example of an implementation of rand() and srand(), possibly useful when one needs the same sequence on two different machines.

       static unsigned long next = 1;

       /* RAND_MAX assumed to be 32767 */
       int myrand(void) {
           next = next * 1103515245 + 12345;
           return((unsigned)(next/65536) % 32768);
       }

       void mysrand(unsigned seed) {
           next = seed;
       }
Mat
  • 202,337
  • 40
  • 393
  • 406
  • This implementation should never be used. It is fundamentally bad. If you do need consistant random number generation between machines, please look at modern methods. A starting point is Mersenne Twister, more modern algorithms can be found in Numerical Recipes **version 3**. – Alexandre C. Mar 09 '11 at 11:04
  • Alternatively, if you don't need the bombastic quality of MT (which is often overkill), you could consider Marsaglia's xor-shift generators. They have 2^32-1 periods in 32 bits and 2^64-1 periods in 64 bits, which usually is enough (for me, anyway). They produce quality pseudorandom numbers (passing diehard), and are ridiculously easy to implement and ridiculously fast. The single caveat is that they generate a sequence of zeroes if seeded with zero (wonder why Marsaglia didn't assess that?), but this can easily be overcome by adding a non-zero constant before doing the shifts and xors. – Damon Mar 09 '11 at 11:17
  • Clarification: I didn't say srand isn't standard! It is! It's behavior isn't :) – Armen Tsirunyan Mar 09 '11 at 15:01
1

As Mat said it is always a good idea to implement the random number generator yourself. Preferably in an object oriented manner. As a nice side effect you can get thread safety and possibly speed besides consistency across platforms. Linear congruential generators http://en.wikipedia.org/wiki/Linear_congruential_generator or mersenne twister http://en.wikipedia.org/wiki/Mersenne_twister will get you far.

Jonas Bötel
  • 4,452
  • 1
  • 19
  • 28
1

I'll add that if you are working under Windows, if you take your exe and move between machines, the srand WILL generate the same numbers, because the implementation of the srand is implementor-specific, but you'll always use the runtime of the same implementor (so if you are using the Microsoft C++, you'll use the srand of Microsoft, and MS won't probably change its implementation of srand today or tomorrow). The same for Linux. Your srand will always be the one of glibc. Unless they change it in glibc, the numbers will be the same.

xanatos
  • 109,618
  • 12
  • 197
  • 280