当前位置:网站首页>Generate random numbers with high quality and Gaussian distribution

Generate random numbers with high quality and Gaussian distribution

2022-04-23 16:53:00 Tassel 1990

Generate random numbers with high quality and Gaussian distribution

1、C# edition

1.1、 Use the following code ( Use 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、 For customized implementation, see (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++ edition

2.1、 Use boost_1_75_0 library

boost::normal_distribution<> nd(dAverage, dVariance);

 

版权声明
本文为[Tassel 1990]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231359253804.html