I am trying to use the C++11 approach to random number generation:
#include <random>
#include <functional>
#include <iostream>
int main(int argc, char *argv[])
{
std::normal_distribution<double> normal(0, 1);
std::mt19937 engine; // Mersenne twister MT19937
auto generator = std::bind(normal, engine);
int size = 2;
engine.seed(0);
normal.reset();
for (int i = 0; i < size; ++i)
std::cout << generator() << std::endl;
std::cout << std::endl;
engine.seed(1);
normal.reset();
for (int i = 0; i < size; ++i)
std::cout << generator() << std::endl;
std::cout << std::endl;
engine.seed(0);
normal.reset();
for (int i = 0; i < size; ++i)
std::cout << generator() << std::endl;
return 0;
}
The output is:
0.13453
-0.146382
0.46065
-1.87138
0.163712
-0.214253
That means that the first and the third sequence are not identical even if they are seeded with the same number. Please, what am I doing wrong? Is the
std::normal_distribution<double>
Just a function in the mathematical sense (produces y out of x deterministically) or am I missing something? If it is just a function, what does the reset method actually do?
You are binding the engine and the distribution, such the following calls on reset won’t affect the bound function.
The solution is to bind references to the engine and the distro