常態分佈隨機數

來源:互聯網
上載者:User

時間關係暫時先不介紹啥是常態分佈了,網上關於產生常態分佈隨機數的方法也有很多。

下面是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<double>(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 domainunsigned 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)之間的隨機數,然後用上面的函數將其轉換成常態分佈的隨機數

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.