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 basing on the normal distribution. As typical for gaussian distribution, the algorithm takes two parameters: mean value and sigma(variance).

#include  
....

double 
GetRandomDoubleUsingNormalDistribution(double mean,
                    double sigma)
{
 typedef boost::normal_distribution NormalDistribution;
 typedef boost::mt19937 RandomGenerator;
 typedef boost::variate_generator GaussianGenerator;

  /** Initiate Random Number generator with current time */
  static RandomGenerator rng(static_cast (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();
}
Category:
Blog Entry
Tags:
, , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>