Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the C++11 standard guarantee identical random numbers for the same seed across implementations?

Tags:

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

like image 707
Joseph Garvin Avatar asked Nov 27 '12 16:11

Joseph Garvin


People also ask

How does c random number generator work?

The random number generator works by remembering a seed value which it uses to compute the next random number and also to compute a new seed. Although the generated numbers look unpredictable within one run of a program, the sequence of numbers is exactly the same from one run to the next.

How to generate random number in embedded C?

Generating IntegersThe rand() function in C could be used in order to generate the random number and the generated number is totally deleting seed. A seed is a value that is used by rand function to generate the random value.


1 Answers

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.

like image 117
Pete Becker Avatar answered Sep 21 '22 05:09

Pete Becker