dimanche 9 juillet 2023

Calling random in secrets module

In the secrets module, they call a specific class of the Random module called Systemrandom. Here is the code from secrets:

from random import SystemRandom

_sysrand = SystemRandom()

randbits = _sysrand.getrandbits
choice = _sysrand.choice

def randbelow(exclusive_upper_bound):
    """Return a random int in the range [0, n)."""
    if exclusive_upper_bound <= 0:
        raise ValueError("Upper bound must be positive.")
    return _sysrand._randbelow(exclusive_upper_bound)

However, when I check the source code of random.py, I only see the randbelow function in the Random class. It does not appear in the Systemrandom class. The relevant code in random.py is:

class Random(_random.Random):
...
class SystemRandom(Random):

So how can the secrets module work when the randbelow is called?




Aucun commentaire:

Enregistrer un commentaire