On this page
secrets.randbelow
Prev: secrets.compare_digest | Up: secrets | Next: secrets.randbits
Rel: secrets.choice, secrets.compare_digest, secrets.randbits, secrets.SystemRandom
Ref: docs.python.org — secrets.randbelow
Tags: #public #python3 #stdlib #secrets
Import
import secrets
from secrets import randbelow
Signature & Overview
secrets.randbelow(exclusive_upper_bound)
Return a random int in the range [0, n).
Usage
import secrets
n = secrets.randbelow(100)
assert 0 <= n < 100
Gotchas
- Exclusive upper bound
[0, n):randbelow(10)returns0..9; passingn <= 0raisesValueError. - Avoid modulo bias: Always use
randbelow(n)instead ofrandbits(k) % n.
REPL Inspect
from secrets import randbelow
help(randbelow)
Linked from
- tuts/python3/stdlib_module/secrets
- tuts/python3/stdlib_module/secrets/choice
- tuts/python3/stdlib_module/secrets/compare_digest
- tuts/python3/stdlib_module/secrets/randbits
- tuts/python3/stdlib_module/secrets/SystemRandom
- tuts/python3/stdlib_module/secrets/token_bytes
- tuts/python3/stdlib_module/secrets/token_hex
- tuts/python3/stdlib_module/secrets/token_urlsafe
Related notes
- tuts/python3/stdlib_module/secrets/choice Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/compare_digest Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/randbits Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/SystemRandom Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/secrets/token_bytes links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/token_hex links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/token_urlsafe links to this page; shared tags: #secrets, #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib