Python Glossary Русский

Python 3.6+secretsfunction

secrets.randbits()

random numbers

Returns a non-negative integer made of k random bits, that is a number in the range [0, 2**k).

Syntax

secrets.randbits(k) -> int

The leading bits may be zero, so the number does not always have exactly k bits: randbits(8) may well return 3. k=0 returns 0 and a negative k raises ValueError. For a range that is not a power of two, use randbelow().

Examples

import secrets
n = secrets.randbits(8)
print(0 <= n < 256)  # → True
print(secrets.randbits(0))  # → 0
print(n)  # → ? случайное число

See also

Open in the full glossarydocs.python.orgReport an inaccuracy