简体   繁体   English

Python随机生成器不是那么随机吗?

[英]Python random generator not so random?

Is the python random generator considered "good"? python随机生成器被认为是“好”吗? As in, does it simulate randomness very well? 如上,它是否很好地模拟了随机性? I made a small program which simulates a person starting at (0, 0) and taking a random step either east, west, north and south. 我编写了一个小程序,该程序模拟了一个从(0,0)开始并随机向东,向西,向北和向南移动的人。 As you run simulations with larger and larger number of steps, you would expect the final position of the person to be closer and closer to the origin. 当您使用越来越多的步数运行模拟时,您会期望人的最终位置越来越靠近原点。 However, as I increased the number of steps in my simulation, the final position was getting farther and farther away from the origin. 但是,随着我增加仿真步数,最终位置离原点越来越远。 The program I used is here: 我使用的程序在这里:

import random

def walk():
    north = 0
    east = 0
    for i in range(10000):
        direction = random.randint(0, 100)
    if direction <= 25:
        north = north + 1
    elif direction > 25 and direction <= 50:
        north = north - 1
    elif direction > 50 and direction <= 75:
        east = east + 1
    else:
        east = east - 1
return north, east



def main():
    for i in range(500):
        north, east = walk()
        print("NE : ", north, east)


main()        

As you run simulations with larger and larger number of steps, you would expect the final position of the person to be closer and closer to the origin. 当您使用越来越多的步数运行模拟时,您会期望人的最终位置越来越靠近原点。

This simply is not true. 这根本是不正确的。 Imagine you start at (0,0). 假设您从(0,0)开始。 You take one step east, taking you to (1,0). 您向东走了一步,到达(1,0)。 Now, in order to get closer to the origin, you need to take a step west. 现在,为了更接近原点,您需要向西走一步。 Taking a step in any other direction will take you further from the origin. 向任何其他方向迈出的一步都会使您离原点更远。 So, you have a 1/4 chance of going closer to the origin and a 3/4 chance of going further away from the origin. 因此,您有1/4的机会靠近原点,而有3/4的机会远离原点。 This is true no matter where you are; 无论您身在何处,都是如此。 more often than not, a step will take you further from the origin. 通常,一步会使您离起点更远。 Thus, your simulation is behaving as expected; 因此,您的模拟行为符合预期。 the longer you go, the further from the origin you get. 您走的时间越长,与原点的距离就越远。


To see why this is true; 了解为什么这是真的; consider a one-dimensional walk, where you can step left or right. 考虑一维步行,您可以向左或向右行走。 If you take ten steps, the expected value is 50:50 left and right; 如果执行十步,则期望值为左右50:50; 5 left and 5 right, taking you back to the origin. 左5和右5,将您带回到原点。 Of course, you might end up with 60:40 or 70:30, leaving you one or two steps away from the origin. 当然,您可能会以60:40或70:30结束,离原点一到两步。 It is a small sample size, after all. 毕竟,这是一个很小的样本。 If you take 1000 steps, you might end up with something like 520 left to 480 right, pretty close to that 50:50 ratio. 如果执行1000步,则最终可能会出现520左右到480左右的情况,非常接近50:50的比率。 If you take 10000 steps, you'll be even closer to the 50:50 ratio, perhaps with something like 5050:4950. 如果迈出10000步,您将更接近50:50的比率,也许是5050:4950。

However, there is something very important to note; 但是,需要注意一些非常重要的事情。 as your sample size gets larger, the proportion of left to right steps is closer to 50:50, but the absolute difference in number between left and right steps gets larger . 随着样本量的增加,左右步长的比例接近50:50,但是左右步长之间的绝对差值也 In that last case, you have a 50.5:49.5 ratio, but you're fifty steps away from the origin, compared to your one step away from the origin in the case where you have a 60:40 ratio with ten steps. 在最后一种情况下,您具有50.5:49.5的比率,但是您与原点的距离是50步,而在您具有60:40的比率和10步的情况下,与原点的距离为50步。

It's not python, it's this line: 这不是python,而是这一行:

        direction = random.randint(0, 100)

The above will generate a value 0 <= N <= 100, which is 101 possible values. 上面将生成一个值0 <= N <= 100,这是101个可能的值。 You have introduced bias into your generator, and given the bias, with larger numbers you will in fact stray further and further from the origin. 您已经将偏见引入了生成器,并赋予了偏见,实际上,您将偏离原点越来越远。

With an unbiased generator, your mean value is zero. 对于无偏发电机,您的平均值为零。

Whatever the above answers, which are all good and correct, the random generator implemented in Python is the Mersenne Twister one which is known to not pass 2 of the hardest tests of the famous application TestU01 and which is known also to be a little big long to evaluate. 不管上面的答案是正确的还是正确的,用Python实现的随机生成器都是Mersenne Twister,众所周知,它没有通过著名应用程序TestU01的最困难的测试中的2个,而且还有些长评估。 You could rather try this library: PyRandLib. 您可以尝试使用以下库:PyRandLib。 See: 看到:

This library contains many of the best-in-class pseudo-random numbers generators while acting exactly as does the Python "built-in" library random. 该库包含许多同类最佳的伪随机数生成器,而其功能与Python“内置”库random完全相同。 Just un-zip or un-tar the downloaded archive in the 'Lib/site-packages/' sub-directory of your Python directory. 只需在Python目录的'Lib / site-packages /'子目录中解压缩下载的档案即可。 And then, enjoy :-) 然后,享受:-)

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

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