简体   繁体   中英

Random Number Generator Explanation

from random import *
def main():
    t = 0
    for i in range(1000):  # thousand
        t += random()
    print(t/1000)
main()

I was looking at the source code for a sample program my professor gave me and I came across this RNG. can anyone explain how this RNG works?

If you plotted the points, you would see that this actually produces a Gaussian ("normal") distribution about the mean of the random function.

在此处输入图片说明

Generate random numbers following a normal distribution in C/C++ talks about random number generation; it's a pretty common technique to do this if all you have is a uniform number generator like in standard C.

What I've given you here is a histogram of 100,000 values drawn from your function (of course, returned not printed, if you aren't familiar with python). The y axis is the frequency that the value appears, the x axis is the bin of the value. As you can see, the average value is 1/2, and by 3 standard deviations (99.7 percent of the data) we have almost no values in the range. That should be intuitive; we "usually" get 1/2, and very rarely get .99999

Have a look at the documentation. Its quite well written: https://docs.python.org/2/library/random.html

The idea is that that program generates a random number 1000 times which is sufficiently enough to get mean as 0.5

The program is using the Central Limit Theorem - sums of independent and identically distributed random variables X with finite variance asymptotically converge to a normal (aka Gaussian) distribution whose mean is the sum of the means, and variance is the sum of the variances. Scaling this by N, the number of X 's summed, gives the sample mean (aka average). If the expected value of X is μ and the variance of X is σ 2 , the expected value of the sample mean is also μ and it has variance σ 2 / N.

Since a Uniform(0,1) has mean 0.5 and variance 1/12, your algorithm will generate results that are pretty close to normally distributed with a mean of 0.5 and a variance of 1/12000. Consequently 99.7% of the outcomes should fall within +/-3 standard deviations of the mean, ie, in the range 0.5+/-0.0274.

This is a ridiculously inefficient way to generate normals. Better alternatives include the Box-Muller method, Polar method, or ziggurat method.

The thing making this random is the random() function being called. random() will generate 1 (for most practical purposes) random float between 0 and 1.

>>>random()
0.1759916412898097
>>>random()
0.5489228122596088

etc.

The rest of it is just adding each random to a total and then dividing by the number of randoms, essentially finding the average of all 1000 randoms, which as Cyber pointed out is actually not a random number at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM