I'm trying to write a small program i C++ using the header in C++11. I use the built-in distributions to generate uniform integer and double distributions. I wrote a very small test:
#include <random>
#include <iostream>
int main()
{
std::mt19937 rng(1234567890);
std::uniform_int_distribution<long int> uniformInt(1,10000);
std::uniform_real_distribution<double> uniformDouble(0.0, 1.0);
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
std::cout.precision(16);
std::cout << rng() << std::endl;
std::cout << uniformInt(rng) << std::endl;
std::cout << uniformDouble(rng) << std::endl;
}
I seed the Marsenne Twister PRNG, with a constant seed, and generate two distributions, one to give a random number between 1 and 10000 and one in the interval 0.0, 1.0, in long int and double respectively. I end the sample by printing a random number directly from the Marsenne Twister, one integer and one double.
My problem comes when I compile this and run it. Using the Clang (clang-900.0.39.2) I get the following output:
2657703298
6144
0.1490309349241722
While if I compile it with g++-7 (Homebrew GCC 7.3.0) I get:
2657703298
3406
0.1490309349241722
The first and last output is the same, while the integer on the interval (1,10000) is not the same.
I'm happy to see the MT produce deterministic random numbers, but at the same time I'm a bit worried that the integer on a uniform distribution is clearly not the same between these two compilers. Is there a small detail I'm missing how these distributions are supposed to work, or is this to be expected?
I think the inclusion of these in C++ is great, but I find this worrisome, as I would like my data to be reproducible between systems/compilers.
Aucun commentaire:
Enregistrer un commentaire