Normal Distribution Random Number

Source: Internet
Author: User

For the time relationship, we will not describe the normal distribution first. There are also many ways to generate a normal distribution random number on the Internet.

The following is the 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;}

So how to use it:

Mo roinvcnd (0.5)

The value corresponding to the cumulative probability of 0.5 is output, that is, 0.

So we can randomly generate a random number between (0, 1) and then use the above function to convert it to a random number with a normal distribution.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.