Yesterday I was looking for some random number generator, based on gaussian distribution. As I don’t like to reinvent the wheel, I started to look for some already existing solutions. I found out that Boost library provided a very powerful engine for generating random numbers using various algorithms. The whole description of Boost Random Number library is available here.
For those who are looking for already existing solutions I attach a small snippet, which generates random numbers based on the normal distribution. As typical for gaussian distribution, the algorithm takes two parameters: mean value and sigma(variance).
#include <boost/random.hpp>
....
double
GetRandomDoubleUsingNormalDistribution(double mean,
double sigma)
{
typedef boost::normal_distribution<double> NormalDistribution;
typedef boost::mt19937 RandomGenerator;
typedef boost::variate_generator<RandomGenerator>, \
NormalDistribution> GaussianGenerator;
/** Initiate Random Number generator with current time */
static RandomGenerator rng(static_cast<unsigned> (time(0)));
/* Choose Normal Distribution */
NormalDistribution gaussian_dist(mean, sigma);
/* Create a Gaussian Random Number generator
* by binding with previously defined
* normal distribution object
*/
GaussianGenerator generator(rng, gaussian_dist);
// sample from the distribution
return generator();
}