使用标准 rand() 函数从标准正态分布中抽取随机数 [英] drawing random number from a standard normal distribution using the standard rand() functions
问题描述
因此大多数编程语言都包含 rand() 功能,您可以通过它生成 0 到 1 之间的随机数......
So most programming languages contain a rand() functionality whereby you can generate a random number between 0 to 1....
我的问题是,有没有办法操纵这个标准的 rand() 功能,以便您能够从具有均值和标准差的正态分布中抽取一个随机数?
My question is, is there a way to manipulate this standard rand() functionality such that you are able to instead draw a random number from normal distribution with a mean and standard deviation?
请注意,我不是问一种语言是否内置正态分布功能,而是问您是否可以使用 rand() 函数模拟"绘制正态分布随机变量...>
Notice that I'm not asking whether or not a language has a normal distribution functionality built in, I'm asking if you can "simulate" drawing a normal distribution random variable using a rand() function...
推荐答案
这里有一个简单有效的方法(来自 此处):
Here is a simple way that works (from here):
static Random r = new Random();
static double Normal(double mu, double sig)
{
double u, v, x, y, q;
do
{
u = r.NextDouble();
v = 1.7156 * (r.NextDouble() - 0.5);
x = u - 0.449871;
y = Math.Abs(v) + 0.386595;
q = Math.Sqrt(x) + y * (0.19600 * y - 0.25472 * x);
} while (q > 0.27597 && (q > 0.27846 || Math.Sqrt(v) > -4.0 * Math.Log(u) * Math.Sqrt(u)));
return mu + sig * v / u;
}
这篇关于使用标准 rand() 函数从标准正态分布中抽取随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!