Skip to content

Use random._randbelow only if available

Clément Pinard requested to merge ClementPinard/pythonfuzz:fix_randbelow into master

For python 3.7+, random._randbelowis not available. It's still available under the name of random._inst._randbelow though. In this MR, the code will try to take random._randbelowand 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.randintor 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

Merge request reports