Use random._randbelow only if available
For python 3.7+, random._randbelow
is not available. It's still available under the name of random._inst._randbelow
though. In this MR, the code will try to take random._randbelow
and fall back to random._inst._randbelow
if it fails to do so.
This is related to #1 (closed)
Note that secrets.randbelow
is much slower than random.randint
or random._inst._randbelow
:
import random
import secrets
import timeit
print(timeit.timeit(lambda: random.randint(0, 100)))
print(timeit.timeit(lambda: random._inst._randbelow(100)))
print(timeit.timeit(lambda: secrets.randbelow(100)))
gives
0.7129823969999904
0.2843797449999954
3.792899893999987