Open In App

Generating random number list in Python

Last Updated : 27 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, we often need to generate a list of random numbers for tasks such as simulations, testing etc. Python’s random module provides multiple ways to generate random numbers. For example, we might want to create a list of 5 random numbers ranging from 1 to 100. This article explores different methods to generate a random number list in Python.

Using random.sample()

random.sample() method is ideal for generating a list of unique random numbers.

Python
import random

# Generate a list of unique random numbers
n = 5  # Number of random numbers
li = random.sample(range(1, 101), n)

print(li) 

Output
[93, 64, 80, 37, 20]

Explanation:

  • random.sample() selects n unique random numbers from the specified range.
  • It ensures that there are no duplicate values in the output.
  • This method is efficient and concise when uniqueness is required.

Let’s explore some more ways and see how we can generate random number list in Python.

Using List Comprehension with random.randint()

random.randint() function can be used in a list comprehension to generate random numbers, including duplicates.

Python
import random

# Generate a list of random numbers (duplicates allowed)
n = 5  # Number of random numbers
li = [random.randint(1, 100) for _ in range(n)]

print(li)

Output
[7, 75, 68, 29, 85]

Explanation:

  • random.randint(1, 100) generates a random number in the range 1 to 100 (inclusive).
  • The list comprehension runs the function n times to create the list.

Using random.choices()

random.choices() function generates a list of random numbers, allowing duplicates and providing more control over probabilities.

Python
import random

# Generate a list of random numbers with potential duplicates
n = 5  # Number of random numbers
li = random.choices(range(1, 101), k=n)

print(li) 

Output
[29, 54, 75, 30, 76]

Explanation:

  • random.choices() generates a list of random numbers of length k.
  • It allows for duplicate numbers in the output.
  • This method is slightly less efficient than random.sample() but useful when duplicates are acceptable.

Using NumPy’s numpy.random.randint()

If we are working with large datasets or need high performance, NumPy provides an efficient way to generate random numbers.

Python
import numpy as np

# Generate a list of random numbers using NumPy
n = 5  # Number of random numbers
li = np.random.randint(1, 101, size=n).tolist()

print(li)

Output
[76, 44, 13, 39, 100]

Explanation:

  • numpy.random.randint() generates random integers efficiently.
  • The size parameter determines the number of numbers generated.
  • Converts the result to a Python list using .tolist().

Using shuffle()

If we need random numbers in a specific range, we can shuffle the range and slice it.

Python
import random

# Generate a random list by shuffling
n = 5  # Number of random numbers
nums = list(range(1, 101))
random.shuffle(nums)
li = nums[:n]

print(li) 

Output
[39, 74, 80, 35, 38]

Explanation:

  • random.shuffle() randomizes the order of elements in a list.
  • Slicing gives the desired number of random numbers.
  • Suitable when the range and size are fixed.


Next Article
Practice Tags :

Similar Reads