I know that the Pseudo random number generator should be initialized only once with one seed. However, in C++, the random number generator and distribution are separated.
Now, Should the distribution function object get created once? Or it doesn't matter? What do I mean is that does it matter to put the distribution object creation call inside or outside when generating random numbers from a distribution. Or it doesn't matter as the distribution function only maps the generator to a number.
The reason I'm asking is because I'm using the generator for generating numbers drawn from several different distributions, it would be nice to put the distribution function object creation call within each function and share the same random number generator.
int main()
{
boost::mt19937 rng(2);
//inside the function
rn_int_1(rng);
rn_int_1(rng);
boost::mt19937 rng2(2);
//outside the function
boost::random::uniform_int_distribution<> six(1,6);
rn_int_2(six,rng2);
rn_int_2(six,rng2);
exit(0);
}
void rn_int_1(boost::mt19937 & generator)
{
boost::random::uniform_int_distribution<> six(1,6);
cout<<six(generator)<<endl;
cout<<six(generator)<<endl;
cout<<six(generator)<<endl;
}
void rn_int_2(boost::random::uniform_int_distribution<> &six,boost::mt19937 & generator)
{
cout<<six(generator)<<endl;
cout<<six(generator)<<endl;
cout<<six(generator)<<endl;
}
The results are:
3
2
1
6
4
6
3
2
1
6
4
6