For example if I instantiate a std::mt19937
with the exact same seed and parameters under GCC and under MSVC, should I get the same sequence of random numbers? If so I assume this property would hold for mersenne_twister_engine in general since mt19937 is just one with specific parameters. This is not true for rand()
in C. It looks like the standard documents the transformations applied in terms of specific code, so I suspect it should always be the same, but the devil is in the details...
Does the C++11 standard guarantee identical random numbers for the same seed across implementations?
Asked
Active
Viewed 1,217 times
23

Joseph Garvin
- 20,727
- 18
- 94
- 165
-
Different implementations of `rand` use different constants so they're never going to match. Wikipedia contains a handy list: http://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use – Mark Ransom Nov 27 '12 at 17:11
-
@MarkRansom: Different implementations are allowed to use totally different algorithms for rand(), constants aside. – Joseph Garvin Nov 27 '12 at 18:58
-
"Allowed to use", sure. In practice they all seem to be the same. – Mark Ransom Nov 27 '12 at 19:10
1 Answers
23
For the new random number engines, yes, for the same seed and parameters you'll get the same sequence of values on all platforms. For rand()
, no. You also don't have that guarantee with random number distributions, even when they are fed the same sequence of input values.

Pete Becker
- 74,985
- 8
- 76
- 165
-
3Also the distributions are not guaranteed to produce the same values, so combining an engine with a distribution will not give identical results across platforms. – bames53 Nov 27 '12 at 17:06
-
C standard, 7.22.2.2/2 "The srand function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand. If srand is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1." How is that not a guarantee? – rici Nov 27 '12 at 17:14
-
Also, for C++, my understanding is that operator== on two generators, adaptors or distributions will be true iff they will generate the same infinite sequences, so it is possible to obtain a guarantee if one exists. – rici Nov 27 '12 at 17:16
-
Simple answer I assume is correct, but is there a convenient cite in the standard for this to make it more complete? – Joseph Garvin Nov 27 '12 at 17:16
-
8@rici - each implementation's version of `rand()` is required to generate the same sequence given the same seed, but there is no requirement that different implementations generate the same sequence. – Pete Becker Nov 27 '12 at 17:17
-
1@JosephGarvin - the requirement is distributed: each engine describes the algorithm that it uses, and, for a particular set of parameters, gives the 10,000th value (handy for checking that the implementation is correct). – Pete Becker Nov 27 '12 at 17:22
-
2pete, ah, true, the algorithm is not defined. @JosephGarvin, the Mersenne twister algorithm (and some other pseudo-random algorithms) are defined in the C++ standard in some detail, in section 26.5.3 (26.5.3.2 for Mersenne twister). – rici Nov 27 '12 at 17:22