Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to get random number in Python

Getting Started with Random Numbers in Python

What is a Random Number?

Before we dive into the technicalities, let's take a moment to understand what we mean by a random number. Imagine you're rolling a dice. The dice is fair, and thus, the outcome of each roll is entirely unpredictable. This unpredictable nature of the outcome is what we refer to as randomness. Similarly, in programming, a random number is a number produced by a process, whose outcome is unpredictable, and which cannot be subsequently reliably reproduced.

Why do we need Random Numbers in Programming?

Random numbers are used in various fields of computing. They're used in simulations, for random sampling in statistics, in cryptography, in games, and even in art. For instance, in games, they can be used to generate unpredictable behavior, create unique elements, or shuffle items.

The Random Library in Python

Python provides the random module in its standard library to perform random generations. Let's import it and explore some functions.

import random

Generating a Random Float

The random() function generates a random float number between 0.0 and 1.0. The function doesn't need any arguments.

random_number = random.random()

Every time you run this block of code, you'll see a different number, always in the range between 0 and 1.

Generating a Random Integer

To generate a random integer in Python, you can use the randint(a, b) function from the random module. It takes two arguments, a and b, and returns a random integer in the range [a, b], including both end points.

random_integer = random.randint(10, 20)

Each time this code is run, you'll see a random integer between 10 and 20 (both inclusive).

Generating a Random Number from a Range

What if you want to generate a random number within a specific range? Here's where randrange(start, stop, step) comes into play. It generates a randomly selected element from the range created by the start, stop and step arguments.

random_number = random.randrange(0, 101, 5)

This will generate a random number from the list of numbers from 0 to 100 (inclusive) that are multiples of 5.

Generating a Random Element from a List

If you have a list of items and you want to pick a random item from that list, you can use the choice() function.

my_list = ['apple', 'banana', 'cherry', 'dates', 'elderberry']
random_item = random.choice(my_list)

Each time you run the above code, a random fruit from the list is printed.

Shuffling Elements

Python’s random module also provides the shuffle() function if you want to randomize the order of the elements in a list.

my_list = ['apple', 'banana', 'cherry', 'dates', 'elderberry']

The Seed Function

The random functions are really pseudo-random, meaning they generate numbers that seem random, but are actually deterministic if you know the algorithm and the seed value. The seed() function allows you to set the initial value used. This can be useful when you want to have a predictable sequence of pseudo-random numbers for testing purposes.


Notice how the two random numbers are the same.


Generating random numbers is a fundamental, yet vital part of programming. Python makes it simple and fun with the random module. As we've seen, this module offers a variety of functions to generate random numbers, select random elements from a list, shuffle elements randomly, and so on. Remember, randomness is everywhere, so the next time you're stuck on a problem, consider if a random component could be your solution!