The first algorithm uses independent normal distributions: generate with law and return
For example, the Box-Muller transform generates two such normal variates from independent uniforms:
To verify this, has density and is an independent uniform angle. Converting the resulting polar density to Cartesian coordinates gives , which factors into two independent normal distributions. Independent pairs therefore give the required sum, discarding a spare coordinate when is odd. Its moment-generating function is , that of the chi-squared distribution.
A genuinely different algorithm uses exponential-envelope rejection sampling for a chi-squared variable. Put . The target and proposal probability density functions are
For , differentiating gives , so the maximum is at . The envelope constant and normalized density ratio are
They remain valid at , where and . Repeatedly draw fresh independent , put , and accept when
The right side is nonpositive by , so this is a legitimate acceptance probability. The joint probability of proposing and accepting in is ; conditional on acceptance the density is exactly . This proves the rejection sampling algorithm, rather than only identifying the target law.
For I prefer the second method: it reduces to a single exponential draw with no rejection. The first method is simple and attractive when normal draws are already available or is small. For larger , the second method avoids generating normals: its acceptance probability is , asymptotic to by Stirling's formula. Its expected number of proposals grows like , rather than the linear number of normal draws in the first method. Both are exact ideal-uniform algorithms; actual runtime also depends on the implementation.