Javascript Random number function learning two: produce random numbers that obey normal distribution

Source: Internet
Author: User

Why do I need to obey the random function of normal distribution

In general, the random number function that we often use math.random () produces a random number that obeys a uniform distribution, can simulate the occurrence of equal probabilities, such as throwing a dice, the probability of 1 to 6 points should be equal, but more random phenomena in real life are in accordance with the normal distribution, For example, a 20-year-old adult's weight distribution.

If we are making a game, to randomly set the height of a lot of NPC, if you also use Math.random (), generate numbers from 140 to 220, you will find that each height segment of the number of people is the same, it is more boring, such a world is also different from our habits, The reality should be particularly high and very short, in the middle of the number of people, which requires random functions to conform to the normal distribution.

Second, normal distribution review

Photo from: http://zh.wikipedia.org/zh-cn/%E6%AD%A3%E6%80%81%E5%88%86%E5%B8%83

Specific properties also refer to the above link, describing the main characteristics of the normal distribution is the mean and variance, for example, the leftmost inverted bell-shaped graph of the mean value is-2, the remaining 0;

The larger the variance, the more flattened the bell shape, the smaller the variance, the steeper;

    • The density function image is symmetric about the mean value.
    • At X=μ±σ, the curve has a inflection point.
    • 68.26% of the area below the function curve is within the range of a standard deviation σ around the average.
    • The area of 95.44% is within the range of the average of two standard deviation 2σ.
    • The area of 99.74% is within the range of the average of three standard deviation 3σ.

When the mean value is 0, the variance is 1 o'clock called the standard normal distribution;

Three, from the uniform distribution through the "Box-muller method" to normal distribution

By reviewing the literature (see: Http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform), there is a known as Box-muller (1958) The converted algorithm can convert two uniform distributions in the interval (0,1] to a standard normal distribution, with the following formula:

Y1 = sqrt (-2 ln (u)) cos (2 pi v)

y2 = sqrt (-2 ln (u)) sin (2 pi v)

Because the trigonometric calculations are slow, we can simplify the calculation by a polarform (polar form) of the above formula,

The algorithm is described as follows:

functiongetnumberinnormaldistribution (mean,std_dev) {returnmean+ (Randomnormaldistribution () *std_dev);}functionrandomnormaldistribution () {varu=0.0, v=0.0, w=0.0, c=0.0;  Do{        //get two independent random variables ( -1,1)U=math.random () *2-1.0; V=math.random () *2-1.0; W=u*u+v*v; } while(w==0.0| | w>=1.0)    //This is Box-muller conversion .C=math.sqrt (( -2*math.log (w))/w);//returns a random number of 2 standard normal distributions, encapsulated in an array to return    //of course, because this function runs faster, you can throw away a    //return [u*c,v*c];    returnu*C;}

Therefore, if we want to obtain a mean value of 180, about 68.26% NPC height is within [170,190], that is 1 standard deviation range, so the standard deviation is 10, can pass getnumberinnormaldistribution (180,10) Call, we experiment 1000000 words and get the result as follows:

// Height: Frequency 128:1132:1133:1134:1135:1136:2137:4138:8139:11140:14141:19142:28143:41144:54145:80146:133147:153148:235149:333150:429151 : 598152:764153:1059154:1314155:1776156:2290157:2835158:3503159:4373160:5513161:6475162:7809163:9437164:11189165:13282166 : 15020167:17239168:19215169:21597170:24336171:26684172:29000173:31413174:33179175:35027176:37084177:38047178:38968179:396 35180:39700181:39548182:38960183:38674184:36948185:35220186:33224187:31038188:29198189:26668190:23893191:21662192:1947619 3:16898194:15056195:13046196:10971197:9456198:7928199:6697200:5370201:4334202:3548203:2810204:2330205:1765206:1350207:109 3208:797209:595210:371211:328212:255213:165214:121215:91216:71217:29218:32219:28220:20221:6222:7223:7224:3225:2228:1

Draw the histogram as follows:

Visible, this is a very obvious normal distribution image feature.

Four, the normal distribution is obtained by uniformly distributed superposition.

We need to sacrifice the universal Central limit theorem.

According to the central limit theorem of independent distribution: Set random variable x1,x2 ... Xn,... Independent, subject to the same distribution, and the mathematical expectation is μ, the standard deviation is σ (σ>0), the sum of the random variables of the normalized variable:

Y= ((X1+X2+...+XN)-nμ)/(sqrt (n) *sqrt (σ)) approximately obeys standard normal distribution N (0,1)

If we add enough more evenly distributed random variables, the sum of the sums will be normal. But how many uniform distributions do we need to accumulate in order to have a good low approximate normal distribution?

Due to X~u (0, 1), μ=1/2,σ=sqrt (1/12)can be obtained, substituting the above formula to approximate the probability density function (P.D.F) of the sum of random variables.

is a P.D.F image that is added by the addition of 2 random variables that obey the U (0,1) Distribution:

What happens if we increase the number of evenly spaced distributions?

Is the n=3 image, you can see the normal distribution of the shape came out, but the top is slightly flat.

Particularly low when n=12 (random variable (X1+X2+...+XN) mean is 6, variance is 1) This time has a good feature, formula y= ((X1+X2+...+XN)-nμ)/(sqrt (n) *sqrt (σ) has a denominator of exactly 1, thus simplifying it to y= ((X1+X2+...+XN)-nμ), which is very easy to program and is already very close to the standard normal distribution, see:

In other words, the mean value is μ, the standard deviation is σ independent of the same distribution variable x1,x2, ..., the arithmetic mean of Xn t= (x1+x2+ ... + Xn)/n, when n is sufficiently large, approximately obeys the average of μ, and the variance is the normal distribution of σ*σ/n.

Finally, the code is as follows:

function getnumberinnormaldistribution (mean,std_dev) {        return mean+ (uniform2normaldistribution () * std_dev);} function uniform2normaldistribution () {    var sum=0.0;      for (var i=0; i<12; i++) {        sum=sum+math.random ();    }     return sum-6.0;}

Similarly, 1 million random numbers will be generated to draw the histogram as follows:

How to produce random numbers that obey uniform distribution?

Please see the previous article:

Javascript Random number function learning one: producing a random number that obeys uniformly distributed

Javascript Random number function learning two: produce random numbers that obey normal distribution

Related Article

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.