---
tag: 8G07Q9SVQ4
kind: statement
role: theorem
name: Box–Muller transform
msc: [65C10, "60E05"]
uses: [0AREFQ9Z65, 05AQE0QR0D, 0GQ938GDE0, 0BSW4T7THS, 00N5FHP1CV, 0E99F51GVB]
about: [05AQE0QR0D]
sources:
  - url: https://en.wikipedia.org/wiki/Box–Muller_transform
same_as:
  wikidata: Q895514
  wikipedia: Box–Muller transform
---

## Statement

Let $U_1$ and $U_2$ be independent random variables, each with the [[uniform distribution|0AREFQ9Z65]] on $(0, 1)$.
Let
$$Z_1 = \sqrt{-2 \ln U_1}\, \cos(2\pi U_2), \qquad Z_2 = \sqrt{-2 \ln U_1}\, \sin(2\pi U_2).$$
Then $Z_1$ and $Z_2$ are [[independent|0GQ938GDE0]], and each has the standard [[normal distribution|05AQE0QR0D]], with mean $0$ and variance $1$.

## Remarks

Let $R = \sqrt{-2 \ln U_1}$ and $\Theta = 2\pi U_2$, so that $(Z_1, Z_2)$ has [[polar coordinates|0BSW4T7THS]] $(R, \Theta)$.
For $t \ge 0$, $P(R^2 > t) = P(U_1 < e^{-t/2}) = e^{-t/2}$, so $R^2$ has the [[exponential distribution|0E99F51GVB]] with mean $2$, and $R$ has density $r e^{-r^2/2}$ on $(0, \infty)$.
Also $\Theta$ is uniform on $(0, 2\pi)$ and independent of $R$.
By the [[change of variables formula|00N5FHP1CV]] with Jacobian $r$, the pair $(Z_1, Z_2)$ has density $\frac{1}{2\pi} e^{-r^2/2} = \frac{1}{\sqrt{2\pi}} e^{-z_1^2/2} \cdot \frac{1}{\sqrt{2\pi}} e^{-z_2^2/2}$, which is the product of two standard normal densities.
Example: $U_1 = e^{-2}$ and $U_2 = 1/8$ give $R = 2$, $\Theta = \pi/4$, and $(Z_1, Z_2) = (\sqrt{2}, \sqrt{2})$.
Independence of $U_1$ and $U_2$ is necessary: with $U_2 = U_1$ the point $(Z_1, Z_2)$ lies on one curve in the plane, so the pair is not a standard normal pair.
To obtain a normal variable with mean $\mu$ and standard deviation $s$, use $\mu + s Z_1$.

## Explanations

### L1 human

The transform turns two uniform random numbers into two independent bell-curve random numbers.
The first uniform number decides the distance from the origin, through a logarithm, and the second decides the direction.
The trick works because the two-dimensional standard normal distribution is the same in every direction, and its squared radius has a simple exponential law.

### L2 human

One cannot invert the normal distribution function in closed form, so plain inverse transform sampling does not give normal variables cheaply; going to two dimensions and polar coordinates solves this.
Marsaglia's polar method avoids the sine and cosine by sampling a uniform point in the unit disc by rejection and rescaling it.
In floating point, $U_1$ must avoid $0$, and the tail of $Z_1$ is truncated by the smallest representable $U_1$.
For speed, many libraries now prefer the ziggurat algorithm, which uses mostly one multiplication and one comparison per sample.
