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

问题描述

我想用C#编写一种方法来生成一个随机数,该随机数的高斯分布范围为[0:1](并且预先为[0-x]).我找到了此代码,但无法正常工作

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));

推荐答案

我写了一篇博客文章,介绍如何生成具有给定分布的随机数:

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. 计算出所需的概率分布函数,以使曲线的一部分下方的面积等于在该范围内随机生成值的概率.
  2. 对概率分布进行积分以确定累积分布.
  3. 求逆累积分布以得到分位数函数.
  4. 通过分位数功能运行它来转换(0,1)上均匀分布的随机数据.
  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