Snorre Jakobsen wrote:
Hello
I'm sorry if this is answered in the documentation, but I didn't find it. I want a function which returns a normally distributed number.
#include
#include using namespace boost;
double SampleNormal (double mean, double sigma) { mt19937 rng; // select random number generator rng.seed((unsigned int) time(0)); // seconds since 1970 // select desired probability distribution normal_distribution<double> norm_dist(mean, sigma); // bind random number generator to distribution, forming a function variate_generator
normal_sampler(rng, norm_dist); // sample from the distribution return normal_sampler(); } This (from http://surl.se/tvk) works very well, except if it's invoked (with same parameters) more often than once a second. If so, the generator gets equal seeds and returns equal values. How can I construct my program so the seeding happens only once?
Hi, Snorre.
Funny! I wrote the very piece of code below this evening!
Make your function a functor, so you initialise the random number generator
only once, in the class' construnctor, but can then use it mulitple times.
Regards,
Angus
#include