W3Schools Python Random randint Guide Generate Random Integers Easily

Logan Jul 13, 2026

W3Schools Python's random.randint() function is an essential tool for generating random integers in Python. This function allows you to introduce an element of unpredictability into your scripts, opening up a world of possibilities for applications such as game development, cryptography, or data shuffling. Let's dive into the details of w3schools python random randint to help you master this function.

5 Random Useful Python Functions
5 Random Useful Python Functions

Imagine you're creating a simple rock-paper-scissors game in Python. To make the game fair and engaging, you'd want the computer's choice to be unpredictable. This is where random.randint() comes in. By using this function, you can ensure that the computer's choice is random, enhancing the user experience. But before we get into the exciting examples, let's understand the basics.

a computer screen with dots on it and the text, create random dots in python
a computer screen with dots on it and the text, create random dots in python

Understanding the Syntax and Parameters

The random.randint() function follows a straightforward syntax: random.randint(a, b). Here, 'a' and 'b' are the lower and upper bounds of the random integer you want to generate, respectively. The function returns an integer between 'a' and 'b', both inclusive.

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

It's essential to understand that random.randint() is not exclusive at the upper end. If you use random.randint(0, 1), you will get either 0 or 1 as outputs, not just 0. This behavior is different from some other languages, so it's a detail worth noting.

Generating a Single Random Integer

a comic strip with an image of a snake and the caption that reads, what people imagine my ball python is like
a comic strip with an image of a snake and the caption that reads, what people imagine my ball python is like

Let's start with a simple example: generating a single random integer between 1 and 10. Here's how you can do it:

```python import random random_integer = random.randint(1, 10) print("Random integer: ", random_integer) ```

Each time you run this code, you'll get a different integer between 1 and 10, inclusive. But what if you want to generate multiple random integers?

Generating Multiple Random Integers

a poster showing the different types of people
a poster showing the different types of people

You can use loops to generate multiple random integers. Here's an example that generates five random integers between 1 and 20, and stores them in a list:

```python import random random_integers = [] for i in range(5): random_integers.append(random.randint(1, 20)) print("Random integers: ", random_integers) ```

With this methodology, you can generate as many random integers as you need. But what about generating a list of unique random integers?

Generating Unique Random Integers

python
python

In some cases, you might want to generate a list of unique random integers. However, random.randint() doesn't guarantee uniqueness. To ensure uniqueness, you can use random.sample() function, which returns a list of unique random integers without replacing the items.

Using random.sample() for Unique Integers

the top 10 python tricks every beginer should know
the top 10 python tricks every beginer should know
Emojis in python
Emojis in python
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
python variables
python variables
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀
Ball Python / Royal Python / Python Regius Morph Visual Chart
Ball Python / Royal Python / Python Regius Morph Visual Chart
Draw a Heart in Python Using Turtle Graphics ❤️
Draw a Heart in Python Using Turtle Graphics ❤️
10 Fun Python Exercises for Kids
10 Fun Python Exercises for Kids
Best Free Websites to Learn Python for Beginners
Best Free Websites to Learn Python for Beginners
Python Turtle graphics
Python Turtle graphics
the top 60 python project ideas
the top 60 python project ideas
a man holding up a poster with the words 5 levels of python on it
a man holding up a poster with the words 5 levels of python on it
Python array sorting guide #python #programming #tutorial
Python array sorting guide #python #programming #tutorial
like how are they so cute
like how are they so cute
a screenshot of the fetch trending searches using python
a screenshot of the fetch trending searches using python
TURTLE DESIGN using Python
TURTLE DESIGN using Python
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30 Python Projects For Beginners | Easy To Advanced Python Project Ideas
30 Python Projects For Beginners | Easy To Advanced Python Project Ideas
we don't sleep we just debug dreams 🙂
we don't sleep we just debug dreams 🙂
💜 Python Trick: Use f-Strings 🐍 Write cleaner and more readable Python code with f-strings ✨
💜 Python Trick: Use f-Strings 🐍 Write cleaner and more readable Python code with f-strings ✨

To generate a list of unique random integers, you can use the random.sample() function like this:

```python import random unique_random_integers = random.sample(range(1, 21), 5) print("Unique random integers: ", unique_random_integers) ```

This will give you a list of five unique random integers between 1 and 20.

Randomizing a List of Integers

Sometimes, you might want to randomize a list of integers. For example, if you have a list of numbers you want to shuffle, you can do so using random.shuffle() function. Here's how:

```python import random numbers = [1, 2, 3, 4, 5] random.shuffle(numbers) print("Shuffled numbers: ", numbers) ```

This will shuffle the order of the elements in the 'numbers' list.

Mastering w3schools python random randint is just the beginning of your journey into random number generation in Python. Once you're comfortable with these functions, you can explore other modules like numpy.random and random, which offer more advanced and specialized functionalities. Happy coding!