On this page
Physics and statistics run on the normal distribution, but a random number generator gives you numbers spread evenly between 0 and 1. The Box–Muller transform bridges the two. G. E. P. Box and Mervin E. Muller published it in 1958 as a two-page note, "A Note on the Generation of Random Normal Deviates", in The Annals of Mathematical Statistics.
What is the Box–Muller transform?
Take two independent numbers U₁ and U₂, each uniform on (0, 1). Compute:
Z₀ = √(−2 ln U₁) · cos(2πU₂)
Z₁ = √(−2 ln U₁) · sin(2πU₂)
Then Z₀ and Z₁ are independent standard normals, with mean 0 and variance 1, as the Box–Muller transform article on Wikipedia sets out. Read the formula in polar form: R = √(−2 ln U₁) is a distance from the origin and Θ = 2πU₂ is an angle, so (Z₀, Z₁) = (R cos Θ, R sin Θ).
For a normal with mean μ and standard deviation σ, use μ + σZ.
Why does the Box–Muller transform work?
Run the argument backwards. Two independent standard normals X and Y have joint density f(x, y) = (1/2π) · e^(−(x² + y²)/2), which depends only on r² = x² + y². The density is perfectly round, the circular case of the hill in our post on the multivariate normal distribution and its contour ellipses.
Switch to polar coordinates. The area element dx dy becomes r dr dθ, so (R, Θ) has joint density (1/2π) · r e^(−r²/2). That is a factor in θ alone times a factor in r alone, so:
Θ is uniform on [0, 2π), which is exactly what 2πU₂ gives.
R is independent of Θ.
P(R > r) = e^(−r²/2), the integral of t e^(−t²/2) from r to infinity.
Point 3 says R² is exponential with mean 2, the waiting-time distribution from our explainer on the memoryless exponential distribution. To sample it, set e^(−R²/2) = U₁ and solve: R = √(−2 ln U₁). Using U₁ where inverse-transform sampling would give 1 − U₁ is fine, because both are uniform on (0, 1). Put R and Θ into x = r cos θ and y = r sin θ, and you have the transform.
A worked example: from (0.3, 0.6) to two normals
Suppose your generator gives U₁ = 0.3 and U₂ = 0.6.
ln 0.3 = −1.2040, so −2 ln U₁ = 2.4079.
R = √2.4079 = 1.5518.
Θ = 2π × 0.6 = 1.2π radians, which is 216°.
cos 216° = −cos 36° = −0.8090, and sin 216° = −sin 36° = −0.5878.
Z₀ = 1.5518 × (−0.8090) = −1.2554.
Z₁ = 1.5518 × (−0.5878) = −0.9121.
Check: Z₀² + Z₁² = 1.5760 + 0.8319 = 2.4079, which equals −2 ln U₁, as it must. A small U₁ gives a large R, so draws close to 0 produce the far tails of the bell curve.
How does the polar (Marsaglia) method avoid sin and cos?
The Marsaglia polar method replaces the angle with a random point. Pick u and v uniform on (−1, 1), and keep the pair only if s = u² + v² lies strictly between 0 and 1, inside the unit circle. Then:
z₀ = u · √(−2 ln s / s)
z₁ = v · √(−2 ln s / s)
Inside the circle, s is uniform on (0, 1) and plays the part of U₁, while u/√s and v/√s are the cosine and sine of a uniform angle, so no trigonometric function is needed. The price is rejection: the circle covers π/4 of the square, so about 21.46% of pairs are thrown away, as the Box–Muller article's comparison of the two forms works out.
Try u = 0.4 and v = −0.3: s = 0.25, accepted. Then −2 ln 0.25 / 0.25 = 11.0904, whose square root is 3.3302, so z₀ = 0.4 × 3.3302 = 1.3321 and z₁ = −0.3 × 3.3302 = −0.9991. A pair like u = 0.9, v = 0.8 gives s = 1.45: draw again.
What goes wrong when you code it?
Before you trust your normals, check these:
U₁ = 0. ln 0 is undefined. If your generator returns values in [0, 1), use 1 − U.
Throwing away Z₁. Both outputs are valid and independent; keep the second for the next call.
Degrees and radians. cos and sin usually expect radians, so use 2πU₂, not 360U₂.
Finite precision. With 32-bit uniforms, the smallest nonzero U₁ caps the output at about 6.66 standard deviations, as the Box–Muller article's section on tail truncation calculates.
Test the output. Check that many draws have mean near 0 and variance near 1.
Normal samples like these drive Monte Carlo methods, including the rare-event tricks in our post on importance sampling for Monte Carlo variance reduction.
If polar coordinates, change of variables or the exponential distribution feel shaky, the Fermi Concept Library gives each concept its own lesson. To practise transformation problems, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer.



