Skip to content

Instantly share code, notes, and snippets.

@lightjiao
Last active September 11, 2023 15:09
Show Gist options
  • Save lightjiao/52795740dfc634f8f8518d1758a8a6d6 to your computer and use it in GitHub Desktop.
Save lightjiao/52795740dfc634f8f8518d1758a8a6d6 to your computer and use it in GitHub Desktop.
正态分布随机数
using System;
public class NormalDistribution
{
private static Random random = new Random();
/// <summary>
/// 理论上这个函数的返回值范围是 正无穷到负无穷,但
/// 约99.7%的数据位于均值的三个标准差范围内 ( 即 mu ± 3sigma )。
/// </summary>
/// <param name="mu"></param>
/// <param name="sigma"></param>
/// <returns></returns>
public static double NextGaussian(double mu = 0, double sigma = 1)
{
// Generate two uniform random variables
double u1 = random.NextDouble(); // Uniform(0,1] random doubles
double u2 = random.NextDouble();
// Use Box-Muller transform to generate two independent standard normally distributed normal variables
// See https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
double R = Math.Sqrt(-2.0 * Math.Log(u1));
double theta = 2.0 * Math.PI * u2;
double z = R * Math.Cos(theta); // Random normal(0,1)
// Scale and shift to get desired mean and standard deviation
return z * sigma + mu;
}
/// <summary>
/// clamp01, include 0 and 1
/// </summary>
/// <returns></returns>
public static double NextGaussian01()
{
return Math.Clamp((NextGaussian() + 4) / 8, 0, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment