Random RevitPythonShell module acting weird?

I use RevitPythonShell and after unsuccessfully finding an error in my code, I found that the problem is due to the random module acting weirdly. Almost all the functions in the random module seem to leave half the capabilities untouched.

random.sample is just a selection of the first half of the list. random.uniform - Generates numbers smaller than the average of two boundaries. random.random () only generates numbers from 0 to 0.5.

The code below is a revitpythonshell terminal. Can someone help me understand what is happening?

>>> 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
>>> 
+4
source share

No one has answered this question yet.

Source: https://habr.com/ru/post/1680083/


All Articles