secrets.choice(range(n, m))
should be fine since range
lazy in Python 3.
n + secrets.randbelow(mn)
is another option. I would not use it since it is less explicitly correct.
Since secrets
provides access to the SystemRandom
class with the same interface as random.Random
, you can also save your instance of SystemRandom
:
my_secure_rng = secrets.SystemRandom()
and do
my_secure_rng.randrange(n, m)
source share