生成随机高质量符合高斯分布的随机数
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

本文介绍如何在C#及C++中生成符合高斯分布的随机数,提供了使用第三方库及自定义实现的方法。
最低0.47元/天 解锁文章
4057

被折叠的 条评论
为什么被折叠?



