RevitPythonShell随机模块表现得很奇怪?

时间:2017-06-25 20:52:39

标签: python random ironpython revitpythonshell

我正在使用RevitPythonShell,经过长时间搜索代码中的错误后,我发现问题是由于随机模块表现得很奇怪。随机模块中的几乎所有功能似乎都没有触及一半的可能性。

random.sample仅对列表的前半部分进行采样。 random.uniform生成的数字小于两个边界的平均值。 random.random()只生成0到0.5之间的数字。

下面的代码是revitpythonshell终端。任何人都可以帮我理解发生了什么吗?

>>> import random
>>> #random number in the range [0.0, 1.0)
>>> for _ in range(1000):
...     if random.random() >= 0.5: print("hello !!")
... 
>>> # nothing got printed !!!
>>> max = 0
>>> for _ in range(1000):
...     rand = random.random()
...     if rand > max: max = rand
... 
>>> max
0.499520565070528
>>> # looks like the random values are being capped at 0.5 !!
>>> 
>>> nums = list(range(10))
>>> for _ in range(1000): 
...     if random.sample(nums, 1)[0] >= 5: print("hello")
... 
>>> # nothing got printed !!
>>> # half of the range of possibilities goes untouched for some reason
>>> a = 3.5
>>> b = 4.75
>>> half = (a+b)/2
>>> for _ in range(1000):
...     if random.uniform(a,b) > half: print("Hello !!!")
... 
>>> # nothing got printed !! all the values are less than half !!!
>>> #looks like all the functions of the random module have this behavior
>>> 

0 个答案:

没有答案