random.randint() Function Explained

random.randint() gives you a random whole number between two values.

It is part of Python’s random module, so you must import random before using it. A very important detail is that random.randint() includes both the start and end values.

Quick answer #

import random

number = random.randint(1, 10)
print(number)

Use random.randint(start, end) to get a random integer between start and end, including both values.

What random.randint() does #

random.randint():

  • Returns a random integer
  • Belongs to Python’s random module
  • Requires import random first
  • Includes both the starting and ending numbers

This makes it useful when you need a random whole number for things like:

  • Dice rolls
  • Guessing games
  • Simple test values
  • Random positions or indexes

If you want a broader introduction to random tools in Python, see the Python random module overview.

Basic syntax #

The basic syntax is:

random.randint(a, b)

Here:

  • a is the smallest possible integer
  • b is the largest possible integer
  • The result can be any whole number from a to b

Example:

import random

number = random.randint(5, 8)
print(number)

Possible output:

7

It could also print 5, 6, or 8.

How the range works #

The range used by random.randint() is inclusive.

That means:

import random

print(random.randint(1, 3))

Can return:

  • 1
  • 2
  • 3

This is different from Python’s range() function, where the stop value is not included.

For example:

print(list(range(1, 3)))

Output:

[1, 2]

This difference causes a lot of beginner mistakes:

  • random.randint(1, 3) includes 3
  • range(1, 3) does not include 3

Simple example #

Here is a simple dice roll example:

import random

roll = random.randint(1, 6)
print("You rolled:", roll)

Example output:

You rolled: 4

The output changes each time you run the program.

This is a common beginner use case because it matches real-world rules:

  • A die has values from 1 to 6
  • Both 1 and 6 should be possible
  • randint(1, 6) fits perfectly

Using random.randint() in real tasks #

Pick a random dice roll #

import random

dice = random.randint(1, 6)
print(dice)

Choose a random number for a guessing game #

import random

secret_number = random.randint(1, 20)
print(secret_number)

You can see this idea in a full beginner project: Python number guessing game example.

Create simple test data #

import random

score = random.randint(0, 100)
print("Test score:", score)

This is useful when you want sample numbers while practicing.

Generate a random index carefully #

You can use randint() to choose an index, but you must be careful to stay inside the valid range.

import random

names = ["Ana", "Ben", "Cara"]
index = random.randint(0, len(names) - 1)

print("Index:", index)
print("Chosen name:", names[index])

Example output:

Index: 2
Chosen name: Cara

If you want a random item from a list, random.choice() is often simpler than generating an index yourself.

Common mistakes #

Forgetting to import the random module #

This causes a NameError.

Wrong:

number = random.randint(1, 10)
print(number)

Fix:

import random

number = random.randint(1, 10)
print(number)

If you run into this, see how to fix NameError: name is not defined.

Assuming the end value is excluded #

Some beginners think this:

random.randint(1, 10)

returns numbers from 1 to 9.

That is incorrect. It can return 10 too.

Passing non-integer values #

randint() is meant for integers.

Problem example:

import random

number = random.randint(1.5, 5.5)
print(number)

This can raise an error because the arguments should be whole numbers.

Use integers instead:

import random

number = random.randint(1, 5)
print(number)

Using a start value larger than the end value #

Problem example:

import random

number = random.randint(10, 1)
print(number)

This can raise a ValueError because the lower value must come first.

Correct version:

import random

number = random.randint(1, 10)
print(number)

When to use randint() vs other random functions #

Use random.randint() when you need a random whole number in an inclusive range.

Choose other functions when the task is different:

  • Use randint() for a random integer like 1 to 6
  • Use random.choice() for a random item from a list
  • Use random.randrange() when you want behavior more like range()
  • Do not use randint() for passwords, tokens, or other security-sensitive code

For security-related randomness, use the secrets module instead of random.

FAQ #

Does random.randint() include the last number? #

Yes. Both the start and end numbers are included.

Can random.randint() return negative numbers? #

Yes. It can return any integer in the given inclusive range, including negative values.

Example:

import random

print(random.randint(-5, 5))

What happens if I use decimal numbers with randint()? #

randint() is for integers. Non-integer values can cause an error.

Do I need to import random first? #

Yes. Use import random before calling random.randint().

Should I use random.randint() for passwords or security tokens? #

No. For security-related randomness, use the secrets module instead.

See also #

Press Esc to close