问题描述
我想要写在C#中的方法来产生一个随机数与高斯范围内分布 [0:1]
(并提前在[0-X]) 。
我发现这个代码,但无法正常工作
随机兰特=新的随机(); //重用这个如果要生成许多
双U1 = rand.NextDouble(); //这些都是一致的(0,1)随机双打
双U2 = rand.NextDouble();
双randStdNormal = Math.Abs(的Math.sqrt(-2.0 *将Math.log(U1))*
Math.Sin(2.0 * Math.PI * U2));
任何身体有想法或经验?
感谢任何紧急援助
我写了一篇关于如何与任何给定的分布产生随机数博客文章:
综上,你想要的算法是:
当然,如果你已经知道你想要的分发点函数,那么你就不需要做这三种感受。
I want to write a method in C# to generate a random number with gaussian distributes in range [0:1]
( and in advance in [0-x] ) .I found this code but not work correctly
Random rand = new Random(); //reuse this if you are generating many
double u1 = rand.NextDouble(); //these are uniform(0,1) random doubles
double u2 = rand.NextDouble();
double randStdNormal = Math.Abs( Math.Sqrt(-2.0 * Math.Log(u1)) *
Math.Sin(2.0 * Math.PI * u2));
Any body has a idea or experience ?
appreciate any urgent help
I wrote a blog post on how to generate random numbers with any given distribution:
http://ericlippert.com/2012/02/21/generating-random-non-uniform-data/
Summing up, the algorithm you want is:
- Work out the desired probability distribution function such that the area under a portion of the curve is equal to the probability of a value being randomly generated in that range.
- Integrate the probability distribution to determine the cumulative distribution.
- Invert the cumulative distribution to get the quantile function.
- Transform your uniformly-distributed-over-(0,1) random data by running it through the quantile function.
Of course if you already know the quantile function for your desired distribution then you don't need to do steps one through three.
这篇关于生成具有高斯分布在0和1之间的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!