Gaussian Random Numbers

A particularly interesting distribution of random numbers is that of Gaussian probability. This is a distribution of random numbers which are generated according to a Gaussian density of points. The latter, with mean $0$ and variance $\sigma =1$, is

\begin{displaymath}
w(x) = ( 2 \pi )^{- \frac{1}{2} } exp( - x^2/2 )
\end{displaymath}

A very efficient method for generating normally distributed variables is to consider a Gaussian distribution in two dimensions ($x_1$, $x_2$) for which the number of points in a differential area is proportional to

\begin{displaymath}
exp( - \frac{1}{2} ( x_1^2 +x_2^2 ) ) dx_1 dx_2
\end{displaymath}

In terms of the usual polar coordinates

\begin{displaymath}
r = ( x_1^2 + x_2^2 )^\frac{1}{2} , \theta = tan^{-1}\frac{x_2}{x_1}
\end{displaymath}

the distribution is

\begin{displaymath}
exp( - \frac{1}{2} r^2 ) r dr d\theta
\end{displaymath}

or, with a further change of coordinate $u = \frac{1}{2} r^2$,

\begin{displaymath}
exp( - u ) du d\theta
\end{displaymath}

Hence, if we generate $u$ between $0$ and $\infty$ with an exponential distribution and $\theta$ uniformly between $0$ and $2 \pi$, then the corresponding values of $x_1$ and $x_2$

\begin{displaymath}
x_1 = ( 2 u )^\frac{1}{2} \cos{\theta}
\end{displaymath}


\begin{displaymath}
x_2 = ( 2 u )^\frac{1}{2} \sin{\theta}
\end{displaymath}

will be distributed normally.

To generate an arbitrary Gaussian distribution with mean $M$ and variance $\sigma$

\begin{displaymath}
w(x) = ( 2 \pi \sigma^2 )^{- \frac{1}{2} } exp( - \frac{(x-M)^2}{\sigma^2} )
\end{displaymath}

we only need to take the numbers generated above, multiply them by $\sigma$, and add to them $M$

The program gaussian_points.c generates such random numbers.

Exercise: The thing to try is to generate ten thousand numbers and plot a histogram of these numbers. To do so write a bin code which accepts the Gaussian numbers from standard input and bin those numbers in arbitrarily chosen bin values. The results of such a filter will be the basis of a histogram distribution of the numbers. You should find that the code gaussian.c generates random numbers centered on $x=0$ and width $\sigma = 1.0$.

Michel Vallieres 2014-04-01