27

I was just wondering if the rand (http://www.cplusplus.com/reference/cstdlib/rand/) function will generate the same sequence of random numbers, while using the same seed, when run on different libc implementations, and even different compilers and operating systems (win, linux).

I did some tests using various compilers( g++4.8, g++5.1 and clang) and it seems that the answer is yes, however I did not find any "official" mention of the PRNG algorithm used in the C's random number generation (http://pubs.opengroup.org/onlinepubs/009604599/functions/rand.html), or whether these should be mentioned in the standards ...

someonewithpc
  • 366
  • 5
  • 14
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
  • 3
    From the link you posted (posix): `If srand() is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated.`. C11, §7.22.2.2 has the same wording. – P.P May 23 '16 at 11:45
  • @P.P. yes, It's just not clear for me whether the sequence will be the same on a different computer too, using a different compiler. – Ferenc Deak May 23 '16 at 11:47
  • 1
    I suspect that you have compared three implementations of the same generator, two of which (the g++ ones) are identical. – molbdnilo May 23 '16 at 11:58
  • 1
    @Lundin Those two discuss how to get consistent results across platforms, but they don't directly discuss whether `rand` has a standard implementation. – pjs May 23 '16 at 13:36
  • 1
    @pjs How is that not the same thing? The latter leads to the former. – Lundin May 23 '16 at 13:37
  • 1
    @Lundin There's a difference between an implicit answer and an explicit one. It's easier for people to miss or overlook the former than the latter. – pjs May 23 '16 at 13:40
  • @pjs I haven't cast any close votes, because I don't know enough about random number generators. However, the sheer amount of duplicates does indicate that this has been asked many times before. – Lundin May 23 '16 at 13:42
  • 1
    @Lundin It's a judgement call. I thought about casting a close vote, but decided having an explicit answer was worth something, in part because of the very first comment indicated how the standard could be misinterpreted. – pjs May 23 '16 at 13:45
  • 2
    @P.P. That only says that a single libc implementation must be consistent between calls to `seed`, not that different implementation must all produce the same sequence. Moreover if that was the meaning then the standard would have to also provide the exact algorithm to produce the "standard sequence" so that people can write conforming implementations, otherwise the request is unreasonable. – Bakuriu May 23 '16 at 16:27
  • Note that at least on OpenBSD, they've made it so rand() returns actual random byes, (so different on each run of the program). I think this violates some standards. https://lwn.net/Articles/625506/ – Buge May 23 '16 at 21:56
  • @Buge: yeah, but the question is about what happens "in C", and since that implementation doesn't conform to the standard it's not C ;-p In practice, almost everyone who needs repeatable random numbers, for modelling or whatever, won't be using `rand()` anyway because there's no guarantee of quality even good enough for a game of Snakes and Ladders. – Steve Jessop May 24 '16 at 01:03

3 Answers3

35

There is no guarantee in the standard about what will be generated:

From the standard:

There are no guarantees as to the quality of the random sequence produced and some implementations are known to produce sequences with distressingly non-random low-order bits. Applications with particular requirements should use a generator that is known to be sufficient for their needs.

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

gnat
  • 6,213
  • 108
  • 53
  • 73
Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98
22

Not even RAND_MAX is specified to have a given value across C implementations other than it must be >= 32767. So rand() on one implementation can return a different range of values than on another and thus a different sequence.

The rand function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX. C11dr §7.22.2.1 2

The value of the RAND_MAX macro shall be at least 32767. C11dr §7.22.2.1 5

RAND_MAX which expands to an integer constant expression that is the maximum value returned by the rand function §7.22 3

Even with the the same RAND_MAX, note @Servé Laurijssen answer: the sequence of values from rand() may differ.


Note: by implication, RAND_MAX <= INT_MAX.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

According to https://stackoverflow.com/a/15500754/1994390 the answer is no. There's no guarantee across different implementations.

Community
  • 1
  • 1
yaakov
  • 5,552
  • 35
  • 48