简体   繁体   English

随机数生成器说明

[英]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. 我正在看教授给我的示例程序的源代码,就遇到了这种RNG。 can anyone explain how this RNG works? 谁能解释这个RNG的工作原理?

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; 按照C / C ++中的正态分布生成随机数,谈到随机数的生成; it's a pretty common technique to do this if all you have is a uniform number generator like in standard C. 如果您所拥有的只是一个统一的数字生成器(如标准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). 我在这里给您的是从函数中提取的100,000个值的直方图(当然,如果您不熟悉python,则返回不打印)。 The y axis is the frequency that the value appears, the x axis is the bin of the value. y轴是值出现的频率,x轴是值的区间。 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. 如您所见,平均值为1/2,通过3个标准差(占数据的99.7%),我们在该范围内几乎没有值。 That should be intuitive; 那应该很直观; we "usually" get 1/2, and very rarely get .99999 我们“通常”获得1/2,很少获得.99999

Have a look at the documentation. 看一下文档。 Its quite well written: https://docs.python.org/2/library/random.html 它写得很好: 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 这个想法是,该程序会生成一个随机数1000次,足以获得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. 该程序使用中央极限定理 -具有有限方差的独立且均匀分布的随机变量X和渐近收敛到正态(aka高斯)分布,其均值是均值的总和,方差是方差的总和。 Scaling this by N, the number of X 's summed, gives the sample mean (aka average). 将其乘以N( X的总和),得到样本均值(即平均值)。 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. 如果预期值X是μ和方差Xσ2,样本的预期值的意思是还μ并且它具有方差σ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. 由于Uniform(0,1)的均值为0.5,方差为1/12,因此您的算法所生成的结果将非常接近于正态分布,均值为0.5,方差为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. 因此,有99.7%的结果应落在平均值的+/- 3标准偏差内,即在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. 更好的替代方法包括Box-Muller方法, Polar方法或Ziggurat方法。

The thing making this random is the random() function being called. 使这个随机的原因是调用random()函数。 random() will generate 1 (for most practical purposes) random float between 0 and 1. random()会在0和1之间生成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. 其余的只是将每个随机数加到总数上,然后除以随机数,从本质上找到所有1000个随机数的平均值,正如Cyber​​指出的那样,这实际上根本不是一个随机数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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