all Python 3randomterm
random.SystemRandom
Random class
A cryptographically secure generator that draws on os.urandom().
Syntax
random.SystemRandom()
Reproducibility is impossible by design: seed() does nothing and getstate()/setstate() raise NotImplementedError, so you cannot pin results for a test — use the ordinary random for that. When the goal is a password, token or key, reach for the secrets module instead: same entropy source, but with ready-made token_hex(), token_urlsafe() and compare_digest().
Examples
import random
srng = random.SystemRandom()
print(srng.random()) # → криптобезопасное [0,1)
print(srng.randint(1, 100)) # → безопасное целое
print(srng.choice(['a','b','c'])) # → безопасный выбор
srng.seed(42) # seed у SystemRandom молча игнорируется: воспроизводимости нет
try:
srng.getstate()
except NotImplementedError:
print('getstate not supported') # → getstate not supported
import os
print(len(os.urandom(16))) # → 16
See also
Open in the full glossarydocs.python.orgReport an inaccuracy