secrets.SystemRandom()
secrets.SystemRandom() is a Python function in the Secrets Module category. A subclass of random.Random using OS-provided cryptographic random source. The syntax is secrets.SystemRandom(). Returns: SystemRandom instance. A typical example: r = secrets.SystemRandom()
r.randint(1, 100) # Secure random integer. A close sibling is secrets.token_bytes(), which returns cryptographically secure random bytes (for tokens, keys, etc.). A close sibling is secrets.token_hex(), which returns a random hex string (for secure tokens). A close sibling is secrets.token_urlsafe(), which returns a URL-safe random string (base64 encoded, no padding). A close sibling is secrets.compare_digest(), which compares two strings in constant time (mitigates timing attacks). More about this category: Cryptographic secure random — token_bytes, token_hex, compare_digest. Related Secrets Module entries: secrets.token_bytes(), secrets.token_hex(), secrets.token_urlsafe(), secrets.compare_digest(), secrets.choice(), secrets.randbelow(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details.