Generate a random number that follows a normal distribution.

Source: Internet
Author: User

First of all, thanks to the szy-8910 blog
Generation of Normal Distribution Random Numbers

The excerpt is as follows:

Ii. Generation of random numbers distributed randomly

The following describes how to generate random numbers with known probability density functions. A typical normal distribution is used as an example to generate random numbers with arbitrary names.

If a random number sequence follows a one-dimensional normal distribution, it has the following probability density functions:

(1-1)

Where μ, σ (> 0) are constants, which are mathematical expectations and mean variance. If you are not clear about the concepts of mathematical expectations and mean variance, please refer to relevant probability theory books. If μ = 0, σ = 0.2, the curve is

Figure 2 probability density function curve of Normal Distribution

We can see that the probability density near μs is large, and the probability density away from μs is small. The random numbers we want to generate must follow this distribution, this means that the probability of the random number to be generated near μs should be large and smaller than μs. The following method can be used to ensure this: points are randomly generated in the large rectangle in Figure 2. These points are evenly distributed. If the generated points fall below the probability density curve, the generated points are considered to meet the requirements, keep them. If they are above the probability density curve, they are considered unqualified and placed. If a large number of points are randomly distributed evenly in the entire rectangle, the abscissa of the points to be preserved follows the normal distribution. It can be imagined that, because the f (x) value at μ is relatively large, the number of points near μ is naturally large, and the number of points close to μ is small, this can be seen from the area. The random number we want to generate is the abscissa here.

Based on the above idea, we can use a program to implement a random number that follows a normal distribution within a certain range. The procedure is as follows:

Double normal (Double X, double Miu, double sigma) // probability density function
{
Return 1.0/SQRT (2 * pI * sigma) * exp (-1*(X-Miu)/(2 * Sigma * sigma ));
}
Double normalrandom (double Miu,
Double Sigma, double min, double max) // generates a random number with a normal distribution
{
Double X;
Double dscope;
Double Y;
Do
{
X = averagerandom (Min, max );
Y = normal (dresult, Miu, SIGMA );
Dsdom = averagerandom (0, normal (Miu, Miu, SIGMA ));
} While (dscope> Y );
Return X;
}

Parameter description: Double Miu: μ, mathematical expectation of a normal function

Double Sigma: σ, mean variance of Normal Functions

Double min, double Max, indicating the range of the generated Random Number

========================================================== ==================================================== ============

// Generate a random number that follows the normal distribution. Double normalrandom (double Miu, double sigma) // generate a random number with the normal distribution {Double X; Double Y; double dscope; double Maxy = normal (Miu, miu, SIGMA); do {x = averagerandom (-3 * Sigma, 3 * sigma); y = normal (x, Miu, SIGMA); dscope = averagerandom (0, maxy) ;}while (DSU> Y); Return X;} double normal (Double X, double Miu, double sigma) // probability density function {return 1.0/SQRT (2 * pI * sigma) * exp (-1 * (X-Miu) /(2 * Sigma * sigma);} int averagerandom (INT min, int max) {srand (time (0); Return (double) (max-min) * rand ()/rand_max + min ;}

It is tested that this method is very inefficient and difficult to generate a qualified vertex. Therefore, this method is not used for random number generation algorithms.

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.