Boost logo

Boost Users :

From: Snorre Jakobsen (snorre_at_[hidden])
Date: 2005-10-12 14:21:16


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 <boost/random.hpp>
#include <time.h>

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<mt19937&, normal_distribution<double> >
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?

My experience with templates is limited, but I do have a grasp of the idea.

Thanks for any hints.

Snorre Jakobsen, Norway


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net