Random numbers are the numbers that are selected by chance from a set of possible values. They are useful for numerous applications, such as statistics, mathematics, simulation, cryptography, gaming, and sampling. These random numbers can be generated using any online tool or software. Python also provides a set of functions/methods that enable users to generate and manipulate a single random number or a list of random numbers. 

This guide will explain all the methods to generate random numbers in Python. 

How to Generate Random Numbers in Python?

Python’s “random” and “numpy” module has various built-in methods that can be used to generate random numbers. These methods include “random()”, “rand()”, “randint()”, “randrange()” and “uniform()” method. 

Let’s review each approach and see how it generates random numbers. 

Approach 1: Generate Random Numbers Using “random()” Method 

The “random()” method is a method in Python’s “random” module used to generate random numbers. This method generates random floating numbers that are uniformly distributed between 0 and 1. 

Here, we are generating a random number using the “random()” method as seen below. This will generate a new number every time we run this snippet:

import random

num1 = random.random()
print(num1)

It can be seen that the random number has been generated in the range between 0 and 1:

Users can also use the “seed()” function and provide a specific seed value to generate a random number based on it. The “seed()” function saves the random function’s state to generate the same random number multiple times on every execution of the code. Here, we have specified the seed value “4” to generate a random number:

import random

random.seed(4)

num2 = random.random()
print(num2)

The below output displays the random number based on the specified seed value:

Approach 2: Generate Random Numbers Using “rand()” Method

The “rand()” method is also used to generate random float numbers in the range from 0 to less than 1. It takes an argument that is considered as a dimension and returns a specified dimension array of random numbers. If no argument is provided, it returns any random float number. For example, we are generating a random number using the “rand()” method without any argument:

import numpy as np

num3 = np.random.rand()
print(num3)

The random float number has been generated as seen below:

Now, we are passing a value “5” to the “rand()” method to define the dimension. This will create a 1D array with five random float values between 0 and 1:

import numpy as np

num4 = np.random.rand(5)
print(num4)

By doing so, the 1D array of five random float values has been generated: 

Approach 3: Generate Random Numbers Using the “randint()” Method

The “randint()” method of the “random” module generates the random integers between the specified range. Here, we are specifying the range “0” and “50” to generate the random number within this range:

import random

num5 = random.randint(0, 50)
print(num5)

The “range()” method with the specified range has generated the following random number:

The “NumPy” library has the “random.randint()” method to generate random numbers within the given range. Here, we are importing the “numpy” library and using the “random.randint()” method:

import numpy as np

num6 = np.random.randint(1, 100)
print(num6)

Upon doing so, the random number has been generated between the specified range:

Users can also specify the “size” parameter in the “randint()” method to get the random numbers in the form of an array. For instance, we are specifying the size value “5” and the range “30”. This will generate five random numbers in the range between “0” to “30”:

import numpy as np

num7 = np.random.randint(30, size=(5))
print(num7)

The below output shows the generated random numbers between the provided range:

Approach 4: Generate Random Numbers Using the “randrange()” Method

The “randrange()” method is also commonly used to generate a random number between the specified range. This method randomly chooses a number from the provided range and returns it. Here, we are generating a random number by specifying the following range to the “randrange()” method:

import random

num8 = random.randrange(5, 15)
print(num8)

It can be observed that the random number has been successfully generated in the specified range:

Approach 5: Generate Random Numbers Using “uniform()” Method 

The “uniform()” method selects a random floating number between the provided limit (numbers) and returns it. For example, we have specified the limits “10” and “20” in the “uniform()” method to generate the random numbers within this limit:

import random

num9 = random.uniform(10, 20)
print(num9)

Subsequently, the random float number has been generated as seen below:

How to Generate a List of Random Numbers in Python?

In Python, users can also generate a list of random numbers. To do so, different approaches can be used. Users can either use the “sample()” method or use the “for” loop to generate random numbers in the form of a list. 

Approach 1: Generate a List of Random Numbers Using the “sample()” Method 

The “sample()” method generates the list of random numbers within the specific range. For instance, we are specifying two parameters to the “sample()” method i.e. range “0” and “30” and the value of random numbers “5”. This will generate the list of 5 random numbers between 0 and 30:

import random

rand_list = random.sample(range(0, 30), 5)
print(rand_list)

The below output displays the list of 5 random numbers from the specified range:

Approach 2: Generate a List of Random Numbers Using “for” Loop

Users can also generate a list of random numbers using the “for” loop. To do so, first, initialize an empty list and then use the “for” loop to append generated random numbers one by one to the empty list. 

For example, we are creating an empty list “rand_list” and using the “for” loop on the range “5” to create a list of 5 random numbers. The “randint(10, 30)” will generate random integers in the range between “10” and “30”. The “append()” method will append the generated random numbers to the “rand_list” list:

import random

rand_list = []

for i in range(5):
    x = random.randint(10, 30)
    rand_list.append(x)

print(rand_list)

It can be seen that the random numbers have been successfully generated using the “for” loop:

How to Select a Random Element from a List in Python?

Sometimes, users may want to select a random number from a particular Python list. Python provides a “choice()” method that randomly selects a number from a list, string, or tuple and returns it. 

For example, we have created a list named “my_list” with some integers. Then, we have used the “choice()” method that will select and return the random number from the created list:

import random

my_list = [3, 1, 72, 15, 6, 9, 38, 64]

num = random.choice(my_list)
print(num)

The below output shows the randomly selected number from the list:

How to Randomly Shuffle a List in Python?

Randomly shuffling a list means rearranging the items of the list. To randomly shuffle a Python list, the “shuffle()” method is used. 

For instance, we have an integer list named “my_list” and we used the “shuffle()” method on it to reorder its items:

import random

my_list = [3, 1, 72, 15, 6, 9, 38, 64]

random.shuffle(my_list)
print(my_list)

The list’s items have been shuffled randomly as seen below:

That was all about generating random numbers in Python. 

Note: Click on the provided link to access Google Colab Notebook. 

Conclusion

In Python, various built-in methods are used to generate random numbers, such as the “random()”, “rand()”, “randint()”, “randrange()” and “uniform()” methods. These methods generate a random number within a specific range. Users can also generate a list of random numbers using the “sample()” method or “for” loop. Moreover, the “choice()” method can also be used to randomly select a number from a particular Python list. This guide has explained all the methods to generate random numbers in Python. 

Frequently Asked Questions

How to generate random integers in Python using randint() method?

You can generate random integers in Python using the randint() method from the random module. This method allows you to specify a range within which the random integer should be generated.

What is the purpose of the seed() function in Python for generating random numbers?

The seed() function in Python is used to set the seed value for the random number generator. By providing a specific seed value, you can ensure that the random number generated remains consistent across multiple executions of the code.

How can I generate random numbers within a specific range in Python?

To generate random numbers within a specific range in Python, you can use the randrange() method. This method allows you to specify the start, stop, and step values to control the range of random numbers generated.

What is the difference between random() and uniform() methods in Python for generating random numbers?

The random() method in Python generates random floating-point numbers between 0 and 1, while the uniform() method generates random floating-point numbers within a specified range. The uniform() method allows you to define the range within which the random number should be generated.

How can I generate random numbers with a specific distribution in Python?

You can use the numpy module in Python to generate random numbers with specific distributions, such as normal distribution, uniform distribution, and more. Numpy provides various functions like np.random.normal() and np.random.uniform() for generating random numbers with different distributions.

Is it possible to generate random numbers without replacement in Python?

Yes, you can generate random numbers without replacement in Python using the random.sample() method. This method allows you to generate a random sample of numbers from a given population without replacement.

How to generate multiple random numbers at once in Python?

You can generate multiple random numbers at once in Python by using the random.choices() method. This method allows you to specify a population and the number of random samples you want to generate from that population.

Can I generate random numbers following a specific probability distribution in Python?

Yes, you can generate random numbers following a specific probability distribution in Python using the scipy.stats module. This module provides various probability distributions like normal, uniform, exponential, and more for generating random numbers with specific distributions.