生成随机高质量符合高斯分布的随机数
1、C# 版本
1.1、使用如下代码(使用 MathNet.Numerics)
double[] createRandom(double mean, double stdDev, int sumCount)
{
Random rand = new Random();
MathNet.Numerics.Distributions.Normal normalDist = new Normal(mean, stdDev);
List<double> resultList = new List<double>();
for (int index = 0; index < sumCount; index++)
resultList.Add(normalDist.Sample());
return resultList.ToArray();
}
1.2、自定义实现可以参见(https://www.itranslater.com/qa/details/2325740528148677632)
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)
2、C++版本
2.1、使用boost_1_75_0库
boost::normal_distribution<> nd(dAverage, dVariance);