Random Gaussian Variables

Sebastian Müller picture Sebastian Müller · Oct 20, 2008 · Viewed 84.7k times · Source

Is there a class in the standard library of .NET that gives me the functionality to create random variables that follow Gaussian distribution?

Answer

yoyoyoyosef picture yoyoyoyosef · Oct 20, 2008

Jarrett's suggestion of using a Box-Muller transform is good for a quick-and-dirty solution. A simple implementation:

Random rand = new Random(); //reuse this if you are generating many
double u1 = 1.0-rand.NextDouble(); //uniform(0,1] random doubles
double u2 = 1.0-rand.NextDouble();
double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) *
             Math.Sin(2.0 * Math.PI * u2); //random normal(0,1)
double randNormal =
             mean + stdDev * randStdNormal; //random normal(mean,stdDev^2)