I am doing some random number generation and getting fishy behaviour. Here is my code:
// initialized earlier... in the constructor of a class
boost::mt19937 *rng = new boost::mt19937();
rng->seed(time(NULL));
// actual use here.
for (int i = 0; i < 10; ++i)
{
test();
}
void test()
{
boost::normal_distribution<> distribution(10, 10);
boost::variate_generator< boost::mt19937, boost::normal_distribution<> > resampler(*rng, distribution);
const double sample = (resampler)(); // always the same value.
}
Am I misusing the random sampling in boost? What have I done wrong to make that always be the same value. I initialize the random number generator in the constructor so it should always spit out a different value (not getting reinitialized)
It is because you are instantiating the class in the function
test
if you move them outside then it works as expected. You are starting off each instance with the same generator. see this minimal case:If you change the loop to work like your posted code then you see the same problem:
The 'why' has been addressed in other answers. Here is how to fix it without re-seeding (which defeat the point of using a generator) : initialize
normal_distribution
andvariate_generator
once, together withmt19937
.In your class, take care of defining theses members in the proper order.
As a side note, 'new' is useless, you could simply write :
The problem is with the line
boost::variate_generator< boost::mt19937, boost::normal_distribution<> > resampler(*rng, distribution);
. This constructor take its parameters by value (see the documentation). So eachresampler
starts off with an identical copy of the generator and calls it once.Edit: Shafik noticed the same thing just a little after I did. If you really can't hoist the initialization out of the loop, you can also re-seed the generator. There are many ways to accomplish this depending on your application. Below is just one example:
Note: do not re-seed
rng
with justtime(NULL)
, as that may return the same value several times if you calltest()
in a tight loop.