all Python 3randomterm
random.randint
integers
Returns a random integer from the range [a, b] inclusive. Both ends are included.
Syntax
random.randint(a, b)
Unlike range and randrange, the upper bound is included — hence the classic off-by-one: random.randint(0, len(items)) will eventually return len(items) and blow up the indexing, you want len(items) - 1. Never use it for passwords, tokens or anything security-related: the stream is predictable from a handful of outputs, so reach for the secrets module instead.
Examples
import random
random.seed(42) # зерно задано — числа воспроизводимы
print(random.randint(1, 6)) # → 6 — бросок кубика, концы включены
print(random.randint(0, 100)) # → 14
print(random.randint(-10, 10)) # → -10
results = [random.randint(1, 6) for _ in range(10)]
print(results) # → [6, 3, 2, 2, 2, 6, 1, 6, 6, 5]
See also
Open in the full glossarydocs.python.orgReport an inaccuracy