random.randint
Pick a random integer from a range.
Python 3.13
random.randint(a, b)Parameters
- aint
- The lower bound of the range.
- bint
- The upper bound of the range; it cannot be less than a.
Return value
An integer from a to b, including both bounds; raises ValueError if a is greater than b.
Examples
Same number on every run
Python 3.13
import random
random.seed(42)
print(random.randint(1, 10))2
Ten dice rolls: both 1 and 6 come up
Python 3.13
import random
random.seed(1)
print([random.randint(1, 6) for _ in range(10)])[2, 5, 1, 3, 1, 4, 4, 4, 6, 4]
Details
Every run gives a new number. To get a repeatable result, call random.seed(...) first, as the first example does. For passwords and tokens, use the secrets module.
