# Marsaglia Transform: Uniform to Standard Normal

The polar method (attributed to George Marsaglia, 1964) is a pseudo-random number sampling method for generating a pair of independent standard normal random variables. While it is superior to the Box–Muller transform, the Ziggurat algorithm is even more efficient. – Wikipedia

This post by Hristo Dachev on the Marsaglia transform sent me down a long road of learning for which I am truly grateful. I learnt about estimating statistics on streaming observations, the Park Miller random number generator, the value of descriptive statistics and maintaining a sorted array. Additionally I ended up creating a histographer (poorly performing in comparison to Hristos) which I then used to visualise the output of the Marsaglia transform.

# The code

/** * Marsaglia transform * http://en.wikipedia.org/wiki/Marsaglia_polar_method * http://blog.controul.com/2009/04/standard-normal-distribution-in-as3/ * @return A number from the standard normal distribution */ private var useCache:Boolean = true; private var cache:Number = 0; final public function stdNormal():Number { useCache = !useCache; if (useCache) return cache; var x:Number, y:Number, w:Number; do { x = float( -1, 1); y = float( -1, 1); w = x * x + y * y; } while (w >= 1 || w == 0); w = Math.sqrt( -2 * Math.log(w) / w); cache = x * w; return y * w; }