The normal (or Gaussian) distribution is one of the most widely used distributions in numerical modelling. It is defined by:
where the two parameters of the distribution are , which is the variance, and , which is the mean of the distribution.
Examples of the practical use of this distribution are widespread:
There are a number of available algorithms for generating normally distributed numbers. A recommended review is Thomas et al., ACM Computing Surveys, Vol. 39, No. 4, Article 11, 2007.
One of the more popular algorithms is the Box-Muller algorithm:
where and are two uniform random numbers between 0 and 1 and is the desired normally distributed random number.
Numbers from the normal distribution can be easily generated using the boost random number library, which is uses the Box-Mulller algorithm described above:
#include <iostream>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>
int main(void)
{
// This is the underlying integer random number generator
boost::mt19937 igen;
// The second template parameter is the actual floating point
// distribution that the user wants
boost::variate_generator<boost::mt19937, boost::normal_distribution<> >
gen(igen,
boost::normal_distribution<>());
const size_t n=10;
for(size_t i=0; i<n; ++i)
{
std::cout<<gen()
<<std::endl;
}
}
As before in the uniform random number chapter (Uniform floating point numbers in the interval 0 to 1), we first need to initialise an integer random number generator (in this case a Mersenne Twister generator), and then use this as the source of randomness in the floating point number generator. Unlike the uniform_01 example however, in this case we need to use an explicit class which connects, called variate_generator.
The line in the program creating the connecting class is:
boost::variate_generator<boost::mt19937, boost::normal_distribution<> >
gen(igen,
boost::normal_distribution<>());
This is a template class which has two template parameters and two parameters to the constructor, in both cases they describe:
Numbers from normal distributions with different parameters and can be generated easily by passing these parameters to the constructor. For example:
boost::variate_generator<boost::mt19937, boost::normal_distribution<> >
gen(igen,
boost::normal_distribution<>(10,0.1));
will generate numbers from a distribution with and .