Back to articles list Articles
8 minutes read

How to Generate Random Numbers in Python

Sometimes you need a random number or element. How can Python help?

The truth is that randomness is all around us. Think about the lottery, a dice roll, or the (extreme) randomness of your office's Secret Santa workings.

In this article, we'll discuss pseudo-randomness, how it's different from true randomness, and how it can be applied in Python to generate random numbers. We'll also delve into some more advanced topics, like reproducible coding with random numbers and using the choice() and choices() functions to return random string elements from a list. Finally, we'll show you how to randomize list order.

Red dice

What Are Pseudo-Random Numbers?

Pseudo-Random Number Generator Numbers

A pseudo-random number generator generates "randomness" with the help of a mathematical algorithm. This means that a random pick is produced with a computer program. To humans, this has the effect of randomness: the result appears completely arbitrary. A pseudo-random generator is almost as good as a true random generator (one that uses an unpredictable physical means to generate random numbers).

For the purposes of this article, when we talk about the randomness that is produced by Python, we're actually talking about pseudo-randomness. It is important to mention that we will not be using a true random generator, but pseudo-randomness is good enough for most of the business world's current needs.

Generating Pseudo-Random Numbers with Python's random Module

One of Python's best-known modules for generating random picks is random. Let's look at the most famous functions from this library.

Generating Pseudo-Random Numbers with Python

Picking a Random Integer

Imagine that you work in sales and you have 10 clients. You want to randomly choose one of these clients for a special offer. You can do a random pick in Python by using the randint() function.

We've created an example?—?10 clients are stored in a Pandas data frame. Each client has an ID and a name. The ID numbers go from 0 to 10 and uniquely identify each client.

import pandas as pd
clients = pd.DataFrame()
clients['client_id'] = [0,1,2,3,4,5,6,7,8,9]
clients['client_name'] = ["Mobili Ltd.","Tymy Ltd.", "Lukas Ltd.","Brod Ltd.", 
"Missyda Ltd.", "Abiti Ltd.", "Bomy Ltd." , "Citiwi Ltd.", "Dolphy Ltd.", "Doper Ltd."]

Now a question arises: How can we use randint() to choose one client from the data frame? Easily:

import random
random_number = random.randint(0,9)
clients.loc[clients['client_id'] == random_number]

In the code above, randint() is called with two arguments: the starting number (which in this case is 0) and the ending number (which is 9). By calling randint() with the arguments 0 and 9, we're telling Python to return a random integer number from 0,1,2,3,4,5,6,7,8, 9. Calling clients.loc[clients['client_id']== random_number] returns the randomly-selected client ID and the associated name:

Selecting a client at random

Try to repeat this process several times. You will not always get the same client because the ID is randomly chosen by the random module.

But what if you want to make your code reproducible? Maybe your boss wants to know how Bomy Ltd. was selected. And maybe he wants to run the code and get the same results again. This is possible because the random module generates pseudo-random numbers, not true random numbers. Randomly-generated numbers in Python can be determined. (There's a mathematical formula behind the choice). We can use the seed() function if we want to get reproducible results.

Reproducible Coding with the seed() Function

Scratch from Piled Higher and Deeper by Jorge Cham

Image credit: "Scratch" from "Piled Higher and Deeper" by Jorge Cham www.phdcomics.com.

The seed() function is used to save the state of the random() function, allowing it to generate the same random number(s) on multiple executions of the code on the same or different machines for a specific seed value. Passing the same seed to random() and then calling that function will always give you the same set of numbers.

If we put random.seed(0) at the beginning of the code in the above example, calling randint(0,9) will always get us the same result:

import random

random.seed(0)
random.randint(0,9)

Using seed() makes your code reproducible; each execution will produce the same result.

The Random Float Number Generator

Another useful function, random(), can be used for a random floating number generator.

Many famous algorithms today use a pseudo-random float number generator in one of their steps. For example, in neural networks, weights are initialized with small random numbers. The idea is to find a perfect set of weights, which a specific mapping function will use to make a good prediction for the output variable. The process of finding such weights is called 'learning';?many iterations of weight combinations are tried and the best combination (i.e. the set of weights that will give the most accurate predictions) is chosen with the help of a type of algorithm called 'stochastic gradient descent'.

The first step in the above process is to use randomly-generated floating numbers. They can be chosen with the help of the random() function. So, if you are in a situation where you need to generate a small number (between 0 and 1), you can call random():

import random
random.random()

This will produce a random number – in our case, 0.5417604303861212.

The returned random number is always between 0 and 1. If you want to get a number from some other interval, you can just multiply your result. The code below will always generate a random number between 0 and 10:

import random
random.random() * 10

There is another option if you want to choose a random number from a specific interval. It's the uniform() function, which takes two arguments: low (the lowest boundary of the interval) and high (the highest boundary of the interval).

If you want to choose a random number between 0 and 10, here's how you can do it with uniform():

import random
radom.uniform(0,10)

Now that we have shown you random() and uniform(), it's up to you which function to use. Either can be used to get a random number within a range.

But what about when you want to randomly choose one element from a tuple or a predefined list? There is also a function for that – it's called choice(). This function is used when you want to randomly select an item from a given list. Let's see how it works.

Using choice() to Return a Random Element from a List

Random Choice

Earlier, we gave the example of a salesman who needs to choose one of his clients from a list. Each client was represented with an integer from 0 to 10. Instead of using randint() to select a random integer, our salesman could just use the choice() function, like this:

import random
import pandas as pd
clients = pd.DataFrame()
clients['client_id'] = [0,1,2,3,4,5,6,7,8,9]
clients['client_name'] = ["Mobili Ltd.","Tymy Ltd.", "Lukas Ltd.","Brod Ltd.", 
                          "Missyda Ltd.", "Abiti Ltd.", "Bomy Ltd." , "Citiwi Ltd.", "Dolphy Ltd.", "Doper Ltd."]

clients.loc[clients['client_id'] == random.choice(clients['client_id'])]

In the code above, random.choice(clients['client_id']) picks a number from the list [0,1,2,3,4,5,6,7,8,9]. The line clients.loc[clients['client_id'] == random.choice(clients['client_id'])] retrieves the client name and ID that was randomly chosen with random.choice(['clients_id']). This is a very elegant way to retrieve a random item.

It's also important to mention that choice() also works with a list of strings. Our salesman could also run random.choice(clients['client_name']) and a random name would be directly returned in the output. Instead of a random integer pick, choice() would be doing a random string pick.

Sometimes you may want to choose several items from a list. This can be done with the choices() function (note the 's'). We can randomly choose two clients from the list by using random.choices(clients['client_name'],k=2). The k argument is used to define the number of elements that we want to randomly select.

The above code will return two randomly chosen names – just keep in mind that if you want to make your code reproducible, you must use the seed() function. (Otherwise, you will always get different names.)

import random
import pandas as pd
clients = pd.DataFrame()
clients['client_id'] = [0,1,2,3,4,5,6,7,8,9]
clients['client_name'] = ["Mobili Ltd.","Tymy Ltd.", "Lukas Ltd.","Brod Ltd.", 
                          "Missyda Ltd.", "Abiti Ltd.", "Bomy Ltd." , "Citiwi Ltd.", "Dolphy Ltd.", "Doper Ltd."]

random.choices(clients['client_name'],k=2)

Randomly Reordering a List with shuffle()

Shuffle

The last function that we are going to mention is shuffle(). You use this function when you need to return all elements from the list but in a different order. Maybe our salesman wants to shuffle his list of clients and use the reordered list to make sales calls. The list of client_ids looks like this: [0,1,2,3,4,5,6,7,8,9]. It can be reordered with random.shuffle(client_id). After executing this line of code, the variable client_id would look something like [0, 4, 3, 2, 8, 9, 1, 6, 7, 5]. We randomly shuffled the list. Now our salesman can make calls by this random reorder.

Shuffle

Of course, we can similarly reorder a list of strings with shuffle(). If we have names stored in a list, like so:

client_name(['Mobili Ltd.', 'Tymy Ltd.', 'Lukas Ltd.', 'Brod Ltd.', 'Missyda Ltd.', 'Abiti Ltd.', 'Bomy Ltd.', 'Citiwi Ltd.', 'Dolphy Ltd.', 'Doper Ltd.'])

We can reorder this list by running random.shuffle(client_name). It returns a shuffled list.

import random
client_name= ["Mobili Ltd.","Tymy Ltd.", "Lukas Ltd.","Brod Ltd.", "Missyda Ltd.", "Abiti Ltd.", "Bomy Ltd." , "Citiwi Ltd.", "Dolphy Ltd.", "Doper Ltd."]
random.shuffle(client_name)
client_name

After calling random.shuffle(), the list was reordered. It would look something like this:

['Abiti Ltd.', 'Citiwi Ltd.', 'Dolphy Ltd.', 'Tymy Ltd.', 'Doper Ltd.', 'Missyda Ltd.', 'Mobili Ltd.', 'Lukas Ltd.', 'Brod Ltd.', 'Bomy Ltd.']

Random Numbers in Python Are Easier Than You Think

One of the most popular Python modules makes pseudo-random number generation easy. Some of the most frequently-used functions from the random module include those that handle a random integer pick (randint()), a random floating number (random(), uniform()), a random list item pick (choice(), choice()) and random list reordering (shuffle()). This article showed you how to use these functions and how to implement randomness in your code. Remember, when you use seed(), these pseudo-random results can be reproduced in your code – a very handy way to put random numbers in Python to work!