Box–Muller transform
A Box-Muller transform (by George Edward Pelham Box and Mervin Edgar Muller 1958) is a method of generating pairs of independent standard normally distributed (zero expectation, unit variance) random numbers, given a source of uniformly distributed random numbers.
It is commonly expressed in two forms. The basic form maps uniformly distributed Cartesian coordinates falling inside the unit circle to normally distributed Cartesian coordinates. The polar form maps uniformly distributed polar coordinates to normally distributed Cartesian coordinates.
One could use the inverse transform sampling method to generate normally-distributed random numbers instead; the Box-Muller transform was developed to be more computationally efficient.[1] The more efficient Ziggurat algorithm can also be used.
Cartesian form
Given x and y independently uniformly distributed in [−1,1], set s = x2 + y2. If s > 1, throw them away and try another pair (x, y), until a pair with s in (0,1] is found. Then, for these filtered points, compute:
and
Polar form
Suppose and are independent random variables that are uniformly distributed in (0,1].
Let
and
Then Z0 and Z1 are independent random variables with a normal distribution of standard deviation 1.
The derivation[2] is based on the fact that, in a two-dimensional cartesian system where X and Y coordinates are described by two independent and normally distributed random variables, the random variables for R2 and (shown above) in the corresponding polar coordinates are also independent and can be expressed as
and
Contrasting the two forms
The Cartesian method as opposed to the polar method is a type of rejection sampling, and throws away some generated random numbers, but it is typically faster than the polar method because it is simpler to compute, provided that the random number generator is relatively fast, and is more numerically robust. It avoids the use of trigonometric functions, which are expensive in many computing environments. It throws away 1 − π/4 ≈ 21.46% of the total input uniformly distributed random number pairs generated, i.e. throws away 4/π − 1 ≈ 27.32% uniformly distributed random number pairs per Gaussian random number pair generated, requiring (1-rejected)-1 = (2 − 4/π)-1 ≈ 1.376 input random numbers per output random number.