28

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 ...

11
  • 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. Commented May 23, 2016 at 11:45
  • 1
    I suspect that you have compared three implementations of the same generator, two of which (the g++ ones) are identical. Commented May 23, 2016 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. Commented May 23, 2016 at 13:36
  • 1
    @pjs How is that not the same thing? The latter leads to the former. Commented May 23, 2016 at 13:37
  • 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. Commented May 23, 2016 at 16:27

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.