the Box-Muller transform
/ BOKS MUL-er /
The normal (bell-curve) distribution is the most-needed shape in all of simulation, yet it is exactly the one inverse-transform sampling struggles with, because its cumulative function has no closed-form inverse. The Box-Muller transform is the neat workaround: it turns two plain uniform numbers into two independent standard-normal numbers using nothing but a logarithm, a square root, and a sine and cosine — a closed-form formula, no root-finding, no special functions.
Here is the whole method. Draw two independent uniforms u_1 and u_2 in (0, 1). Compute R = sqrt(-2 * ln(u_1)) and theta = 2 * pi * u_2. Then z_1 = R * cos(theta) and z_2 = R * sin(theta) are two independent draws from the standard normal N(0, 1). The trick exploits a beautiful fact: if you place a 2D Gaussian on the plane, its squared radius R^2 is exponentially distributed and its angle theta is uniform — completely independent of each other. So sampling the radius (via the exponential, which inversion handles easily as -2 ln u_1) and the angle (uniform) and converting from polar back to Cartesian coordinates lands you on a genuine 2D Gaussian, whose two coordinates are independent normals.
It is exact, short, and a staple of random-number libraries. Two honest footnotes. First, the trigonometric version is correct but the sin and cos can be slow; the 'polar' (Marsaglia) variant avoids them by a small rejection step, and modern libraries often use the faster Ziggurat algorithm instead. Second, beware a subtle floating-point pitfall: if u_1 can equal exactly 0, then ln(0) is minus infinity and the result is garbage, so implementations draw u_1 from (0, 1] or guard against the zero. Once you have standard normals, scaling and shifting (mu + sigma * z) gives any normal you want.
Take u_1 = 0.5, u_2 = 0.25. Then R = sqrt(-2 * ln 0.5) = sqrt(1.386) = 1.177 and theta = 2 * pi * 0.25 = pi/2. So z_1 = 1.177 * cos(pi/2) = 0 and z_2 = 1.177 * sin(pi/2) = 1.177 — two standard-normal draws from one pair of uniforms.
Two uniforms in, two independent normals out — via a radius and an angle.
If u_1 can be exactly 0, ln(u_1) is minus infinity and the sample is corrupt — draw from (0,1] or guard the zero. The basic trig version's sin/cos cost has led most libraries to the polar variant or the Ziggurat method.