Normal Distribution of random number generator in C #
For more information, see numerical recipes in C ++ 2/e p.292 ~ P.294 and Simulation Modeling and Analysis
3/e p.465 ~ P.466.
Box and Muller provided in 1958 that a random variable with a normal distribution is generated from a uniformly distributed random variable.Algorithm. Where U1 and U2 are random variables with a uniform distribution on the intervals (0, 1), they are independent of each other. Ling
X1 = SQRT (-2 * log (U1) * Cos (2 * pI * U2 );
X2 = SQRT (-2 * log (U1) * sin (2 * pI * U2 );
Therefore, X1 and X2 are subject to the N () distribution and are independent of each other. We use two independent random numbers U (0, 1) to obtain two independent random numbers N (0, 1.
First, Gaussian random numbers have the following features: The mean value is 0, and the variance is 1. This is relative to the random number of the standard distribution,
Simply put, the difference between them is that the probability of random numbers being generated within the interval is different. Specifically
The probability of a region is large, and the probability of a region is small. There is a standard distribution of images on it,
As for the Gaussian probability distribution image, I think it can be found on the Internet, or on mathematics textbooks.
This is the first and recognized by knuth to generate a Gaussian random series.Code:
# Include <stdlib. h>
# Include <math. h>
Double gaussrand ()
{
Static double V1, V2, S;
Static int phase = 0;
Double X;
If (phase = 0)
{
Do
{
Double U1 = (double) rand ()/rand_max;
Double U2 = (double) rand ()/rand_max;
V1 = 2 * U1-1;
V2 = 2 * U2-1;
S = V1 * V1 + V2 * V2;
}
While (S> = 1 | S = 0 );
X = V1 * SQRT (-2 * log (s)/s );
}
Else
X = V2 * SQRT (-2 * log (s)/s );
Phase = 1-phase;
Return X;
}
The above Code is based on the box-Muller method. The basic idea is to generate two independent random numbers U and V, which are evenly distributed on (0, 1, use U and V to generate two sets of independent Standard Normal Distribution Random Variables X and Y:
-
.
This equation is proposed because of the chi-square distribution of the two degrees of freedom (see property 4) is easily generated by exponential random variables (lnu in the equation. Therefore, using the random variable V, you can select an even angle around the circle, and use the exponential distribution to select the radius and then convert it to the (Normal Distribution) X and Y coordinates.