C/C++ algorithm to produce same pseudo-random number sequences from same seed on different platforms?
c, c++, consistency, portability, random
Solution
Any particular pseudo-random number generation algorithm will behave like this. The problem with `rand` is that it's not specified how it is implemented. Different implementations will behave in different ways and even have varying qualities.
However, C++11 provides the new `<random>` standard library header that contains lots of great random number generation facilities. The random number engines defined within are well-defined and, given the same seed, will always produce the same set of numbers.
For example, a popular high quality random number engine is `std::mt19937`, which is the Mersenne twister algorithm configured in a specific way. No matter which machine, you're on, the following will always produce the same set of real numbers between 0 and 1:
std::mt19937 engine(0); // Fixed seed of 0
std::uniform_real_distribution<> dist;
for (int i = 0; i < 100; i++) {
std::cout << dist(engine) << std::endl;
}
Problem
The title says it all, I am looking for something preferably stand-alone because I don't want to add more libraries. Performance should be good since I need it in a tight high-performance loop. I guess that will come at a cost of the degree of randomness.