本文介绍了生成具有高斯分布在0和1之间的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要写在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));



任何身体有想法或经验?



感谢任何紧急援助


解决方案

我写了一篇关于如何与任何给定的分布产生随机数博客文章:





综上,你想要的算法是:




  • 锻炼所需的概率分布函数使得该曲线的一部分下的面积等于在该范围内随机生成一个值的概率。

  • 整合的概率分布来确定的累积分布

  • 反转累积分布得到的位数函数

  • 将通过位数函数运行它变换你的均匀分布,过(0,1)随机数据



  • 当然,如果你已经知道你想要的分发点函数,那么你就不需要做这三种感受。


    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:

    1. 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.
    2. Integrate the probability distribution to determine the cumulative distribution.
    3. Invert the cumulative distribution to get the quantile function.
    4. 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之间的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-22 07:43