Student Handbook

Student Handbook

Open in ChatGPT
Ask ChatGPT about this page
Open in Claude
Ask Claude about this page

random.randint

Picks a random whole number between two numbers.

Synopsis

import random
random.randint(a, b)

Description

Both ends are included: randint(1, 6) can give 1, 6, or anything between. That is different from range, which stops before its end.

Parameters

Name Meaning
a The smallest number allowed.
b The largest number allowed.

Return value

An int.

Example

import random

for i in range(5):
    print(random.randint(1, 6))

It prints something like this. Your values will differ: they are random.

5
4
3
3
3

Common mistake

import random

print(random.randint(6, 1))

Python stops with:

ValueError: empty range in randrange(6, 2)

a must not be bigger than b. Write the smaller number first.

See also

random.choice, range

random.randint in the Python documentation

Last updated 2 weeks ago