Skip to content

Instantly share code, notes, and snippets.

@mikedugan
Created January 3, 2014 05:15
Show Gist options
  • Save mikedugan/8233170 to your computer and use it in GitHub Desktop.
Save mikedugan/8233170 to your computer and use it in GitHub Desktop.
Box-Mueller method
public static double NextGaussianDouble(this Random r)
{
double U, u, v, S;
do
{
u = 2.0 * r.NextDouble() - 1.0;
v = 2.0 * r.NextDouble() - 1.0;
S = u * u + v * v;
}
while (S >= 1.0);
double fac = Math.Sqrt(-2.0 * Math.Log(S) / S);
return u * fac;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment