Wednesday 1 June 2016

c# - Random Gaussian Variables




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


Answer



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)

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...