We have the following code to generate normally distributed random numbers.
double average = 10.0; double spread = 4.0;
std::mt19937 generator;
generator.seed(1);
std::normal_distribution<double> distribution(average, spread);
for (uint i = 0; i < 10; i++)
cout << "randomNumber=" << distribution(generator) << endl;
This should produce the same exact sequence on Linux and Windows. But it doesn't. The sequences are identical when rerun on the same OS. But the sequences are different when run on Linux vs. Windows. The generators are the same and the seeds are the same, so they should give identical sequences.
What's missing or incorrect that would allow the code to create identical sequences on both operating systems?
Windows 10. Visual Studio 2010.
Ubuntu 14.04. g++ (gcc 4.9.2)
Windows Output:
randomNumber=10.6243
randomNumber=11.2256
randomNumber=7.72784
randomNumber=8.30245
randomNumber=6.77485
randomNumber=9.18181
randomNumber=5.19982
randomNumber=8.28505
randomNumber=5.24899
randomNumber=15.2219
Linux Output:
randomNumber=7.80102
randomNumber=4.38851
randomNumber=16.331
randomNumber=5.81941
randomNumber=11.0304
randomNumber=2.16242
randomNumber=3.96877
randomNumber=8.73883
randomNumber=13.4327
randomNumber=10.2854
The C++ library specification guarantees that the generator will produce the same sequence of values, but the distributions are not required to.
If you modify your code to produce raw output from the mt19937 generator you will find that the results are the same across platforms:
std::mt19937 generator(1);
for (uint i = 0; i < 10; i++)
cout << "randomNumber=" << generator() << endl;
It certainly would be useful if the distributions were also deterministic across platforms, but I imagine the committee felt that would take away too much freedom from implementers to implement new algorithms for producing distributions.
Anyway, if you want deterministic distributions you'll need to implement them yourself or use another library that provides deterministic cross-platform implementations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With