Box-Muller Transform Visualization

The Box-Muller transform is a random number sampling method that converts a pair of uniformly distributed random numbers into a single (or if you'd rather, a pair1 of) normally distributed random number(s).

The default choices of the function definitions producing r, θ, and n in this visualization produce a standard normal distribution. Changing the functions can significantly alter the distribution of the transformed random numbers. Play around with different combinations of functions to see how they affect the output.


Step 1: Generate cartesian points

Uniformly generate random values U₁,U₂ [0, 1].
Plot points (U₁,U₂) on the cartesian plane.
 
Click and drag to see a single point transform.

Step 2: Project to polar coordinates

Project each point (U₁,U₂) to a polar coordinate (r, θ).
Define r (the radial distance) as a function of U₁.
Define θ (the angle from x axis) as a function of U₂.
Click and drag to see a single point transform.

Step 3: Map polar coordinates to scalars

Define a function f(r, θ) producing a scalar value n.
Move the distribution center by changing the mean.
Widen or narrow the distribution by changing the stddev.
Click and drag to see the points that map to the range.
No scalar expression selected

[1] This transform can actually creates a pair of independent and normally distributed values from the inputs U₁ and U₂. We consistently use the cosine function to create one value of the pair for this visualization. The other value can be created by swapping cosine out for sine.