The polar form of the Box-Muller transformation for calculating random variables with a Gaussian (or Normal) distribution.
on GetGausseRandom (mean, standardDeviation) -- returns a standard Gaussian random number -- based upon the polar form of the Box-Muller transform w = 2.0 repeat while ( w >= 1.0 ) x1 = 2.0 * (random(0,1000000)/1000000.0) - 1.0 x2 = 2.0 * (random(0,1000000)/1000000.0) - 1.0 w = x1 * x1 + x2 * x2 end repeat w = sqrt( (-2.0 * log( w ) ) / w ) r = (x1 * w) if voidP(mean) then return r return (mean + r * standardDeviation) end
This function can be useful for creating 'weighted' randomness - ie random values that tend to congregate around an 'average' rather than being evenly spread across the range of possibilities. Here's an example of using Gaussian pseudo-random numbers to make piles of snow. Source Movie
First published 26/04/2005