I use the following code to test the C++ <random>
library.
Why do I get the exact same sequence for every run of the compiled executable? Is rd()
deterministic upon compilation? How do I get different output for each run?
GCC 4.8.1 on Windows 7 64bit. Using MinGW distribution from http://nuwen.net/mingw.html.
EDIT: I tested the same piece code with Visual Studio. There is no problem. The outputs are non deterministic. This could be a bug in mingw gcc 4.8.1 that I used.
#include <iostream>
#include <random>
using namespace std;
int main(){
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dist(0,99);
for (int i = 0; i< 16; ++i){
cout<<dist(mt)<<" ";
}
cout <<endl;
}
rd.entropy()
? – Brottentropy() == 0
. If it does, that’s a bug. – Ceriserandom_device
somehow though it doesn't get parameters! – Macawrandom_device
works. – Cerise_GLIBCXX_USE_RANDOM_TR1
please? If it’s 0, then it’s using mt19937 with a fixed seed as a fallback. – Ceriserand_s
as an entropy source has been added recently to libstdc++, though it also seems to supportRDRAND
andRDSEED
. I think the default is picked through preprocessor definitions during library comping, and possibly overridden by a token string passed through the constructor. Honestly, I find reading the libstdc++ code to be a headache. I cannot tell if rand_s would be the default on Windows (as it should have always been!) – Fari