Generating random number list in Python
Last Updated :
27 Jan, 2025
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.
Similar Reads
Python Generate Random Float Number Generating a random float number in Python means producing a decimal number that falls within a certain range, often between 0.0 and 1.0. Python provides multiple methods to generate random floats efficiently. Letâs explore some of the most effective ones.Using random.random()random.random() method
2 min read
Sum of number digits in List in Python Our goal is to calculate the sum of digits for each number in a list in Python. This can be done by iterating through each number, converting it to a string, and summing its digits individually. We can achieve this using Pythonâs built-in functions like sum(), map(), and list comprehensions. For exa
2 min read
Generate a List of Random Numbers Without Duplicates in Python There are a few ways to generate a list of random numbers without duplicates in Python. Letâs look at how we can generate a list of random numbers without any duplicates.Using random.sample()This random.sample() method allows us to generate a list of unique random numbers in a single step. Pythonimp
2 min read
Python - Convert Number to List of Integers We need to split a number into its individual digits and represent them as a list of integers. For instance, the number 12345 can be converted to the list [1, 2, 3, 4, 5]. Let's discuss several methods to achieve this conversion.Using map() and str() Combination of str() and map() is a straightforwa
3 min read
Create List of Numbers with Given Range - Python The task of creating a list of numbers within a given range involves generating a sequence of integers that starts from a specified starting point and ends just before a given endpoint. For example, if the range is from 0 to 10, the resulting list would contain the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8
3 min read