I was surprised to see that the output of this program:
#include <iostream> #include <random> int main() { std::mt19937 rng1; std::mt19937 rng2; std::uniform_real_distribution<double> dist; double random = dist(rng1); rng2.discard(2); std::cout << (rng1() - rng2()) << "\n"; return 0; }
is 0 - that is, std::uniform_real_distribution uses two random numbers to create a random double value in the range [0,1]. I thought it would just generate one and rescale it. Thinking about this, I assume that this is due to the fact that std::mt19937 creates 32-bit int and double twice this size and, therefore, is not "random enough".
Question: How to find out this number in the general case, i.e. if random number generator and floating point type are arbitrary types?
Edit: I just noticed that instead of std::generate_canonical I could use, since I am only interested in random numbers [0,1]. Not sure if that matters.
source share