首页 > 编程知识 正文

从标准正态分布中随机产生100个随机数,产生服从正态分布的随机数

时间:2023-05-03 09:32:07 阅读:205060 作者:3293

时间关系暂时先不介绍啥是正态分布了,网上关于生成正态分布随机数的方法也有很多。

下面是moro 逆正态累积分布函数(Moro's Inverse Cumulative Normal Distribution function)

double MoroInvCND(double prob)

{

const double a1 = 2.50662823884;

const double a2 = -18.61500062529;

const double a3 = 41.39119773534;

const double a4 = -25.44106049637;

const double b1 = -8.4735109309;

const double b2 = 23.08336743743;

const double b3 = -21.06224101826;

const double b4 = 3.13082909833;

const double c1 = 0.337475482272615;

const double c2 = 0.976169019091719;

const double c3 = 0.160797971491821;

const double c4 = 2.76438810333863E-02;

const double c5 = 3.8405729373609E-03;

const double c6 = 3.951896511919E-04;

const double c7 = 3.21767881768E-05;

const double c8 = 2.888167364E-07;

const double c9 = 3.960315187E-07;

double z;

bool negate = false;

const double maxValue = static_cast(0xffffffffUL);

// Ensure the conversion to floating point will give a value in the

// range (0,0.5] by restricting the input to the bottom half of the

// input domain. We will later reflect the result if the input was

// originally in the top half of the input domain

unsigned int x = (unsigned int)(prob * maxValue);

if (x >= 0x80000000UL)

{

x = 0xffffffffUL - x;

negate = true;

}

// x is now in the range [0,0x80000000) (i.e. [0,0x7fffffff])

// Convert to floating point in (0,0.5]

const double x1 = 1.0 / maxValue;

const double x2 = x1 / 2.0;

double p1 = x * x1 + x2;

// Convert to floating point in (-0.5,0]

double p2 = p1 - 0.5;

// The input to the Moro inversion is p2 which is in the range

// (-0.5,0]. This means that our output will be the negative side

// of the bell curve (which we will reflect if "negate" is true).

// Main body of the bell curve for |p| < 0.42

if (p2 > -0.42)

{

z = p2 * p2;

z = p2 * (((a4 * z + a3) * z + a2) * z + a1) / ((((b4 * z + b3) * z + b2) * z + b1) * z + 1.0);

}

// Special case (Chebychev) for tail

else

{

z = log(-log(p1));

z = - (c1 + z * (c2 + z * (c3 + z * (c4 + z * (c5 + z * (c6 + z * (c7 + z * (c8 + z * c9))))))));

}

// If the original input (x) was in the top half of the range, reflect

// to get the positive side of the bell curve

return negate ? -z : z;

}

那么如何使用呢:

MoroInvCND(0.5)

输出的是累积概率到0.5的那个对应的值,即0。

所以可以先随机生成(0,1)之间的随机数,然后用上面的函数将其转换成正态分布的随机数

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。