dimanche 25 juin 2017

RevitPythonShell random module acting weird?

I am using the RevitPythonShell and after a frustratingly long search for a bug in my code, I found that the problem was due to the random module acting weird. Almost all the functions in the random module seem to leave half of the possibilities untouched.

random.sample is only sampling the first half of the list. random.uniform is generating numbers less than the average of the two bounds. random.random() is only generating numbers between 0 and 0.5.

Below code is form the revitpythonshell terminal. Can anyone help me understand what is going on ?

>>> 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
>>> 




Aucun commentaire:

Enregistrer un commentaire