Generate independent and use the Box-Muller transform
For , , so its density is on . The angle is uniform on and independent of . Dividing the joint radial-angular density by the polar-coordinate Jacobian gives
Thus both outputs are independent and have the standard normal distribution. Independent pairs of uniforms give further independent normal observations. The null event is excluded in implementation so the logarithm is finite.
Recognize the integrand as a Gaussian scale mixture. If has the unit Rayleigh distribution and is an independent standard normal distribution variable, then the conditional density of given is . Multiplying by the radius density gives
Thus use for the independent radius and for the normal output of the Box-Muller transform:
The mixture argument also proves that integrates to one, by the Tonelli theorem. As a check, is exponential of rate , so the characteristic function of is . This is the Rayleigh-normal scale mixture, with Laplace distribution density .
The Box-Muller transform uses independent uniforms to set
The radius has the unit Rayleigh distribution, with density for , and the angle is independently uniform on . Their joint density is . The Cartesian change of variables has absolute Jacobian determinant , so the joint density of is
The factorization proves that the outputs are independent random variables, each with a standard normal distribution.
Draw independent uniformly on , and set
This is the Box-Muller transform. The endpoint has probability zero; an implementation should avoid evaluating its logarithm.
For , , so has density . It is independent of the angle, which has a uniform distribution on , and their joint density is . Transforming to Cartesian coordinates divides by the polar-coordinate Jacobian , yielding
Thus the outputs are independent random variables with the standard normal distribution, proving the algorithm.