How to randomly select elements of an array with NumPy in Python ?
Last Updated :
28 May, 2025
Randomly selecting elements from an array means choosing random elements from the array. NumPy offers several efficient methods to pick elements either with or without repetition. For example, if you have an array [1, 2, 3, 4, 5] and want to randomly select 3 unique elements, the output might look like [1 5 2]. Let’s explore different methods to do this efficiently.
Using numpy.random.choice()
This method randomly selects elements from an array without repetition. It’s useful when you need unique random samples of a given size.
Python
import numpy as np
a= np.array([1, 2, 3, 4, 5])
res = np.random.choice(a, size=3, replace=False)
print(res)
Explanation: np.random.choice(a, size=3, replace=False)selects 3 unique random elements from the array a, where size=3 means 3 values are chosen, and replace=Falseensures no repetition.
Using numpy.random.shuffle()
This shuffles the array in-place, meaning the original array is modified. It's very fast and memory-efficient; just take the first n values after shuffle.
Python
import numpy as np
a= np.array([1, 2, 3, 4, 5])
np.random.shuffle(a)
res = a[:3]
print(res)
Explanation: np.random.shuffle(a) randomly shuffles the elements of array a in-place, changing the original order. res = a[:3] then selects the first 3 elements from the shuffled array.
Using numpy.random.permutation()
This method returns a new shuffled copy of the original array. The original array remains unchanged, making it safer for reuse.
Python
import numpy as np
a= np.array([1, 2, 3, 4, 5])
b = np.random.permutation(a)
res = b[:3]
print(res)
Explanation: np.random.permutation(a) returns a shuffled copy of array a without modifying the original. res = b[:3] selects the first 3 elements from this shuffled copy.
Using replace=True
This allows sampling with repetition, so the same value can appear multiple times. It’s useful when you need more samples than unique elements.
Python
import numpy as np
a= np.array([1, 2, 3, 4, 5])
res = np.random.choice(a, size=3, replace=True)
print(res)
Explanation: np.random.choice(a, size=3, replace=True) randomly selects 3 elements from array a, allowing repetition. This means the same element can appear multiple times in the result.
Related articles
Similar Reads
How to randomly select rows of an array in Python with NumPy ? In this article, we will see two different methods on how to randomly select rows of an array in Python with NumPy. Let's see different methods by which we can select random rows of an array: Method 1: We will be using the function shuffle(). The shuffle() function shuffles the rows of an array rand
2 min read
How to Select a Random Element from a Tuple in Python Selecting a random element consists of retrieving one element from a collection in an unpredictable manner. In Python, this can be done easily using different methods provided by the random module. Below are the three different approaches to select a random element from a tuple. Select a Random Elem
2 min read
How to Select a Random Element from Array in TypeScript ? We are given an array in TypeScript and we have to select a random element every time the code runs. It will automatically select a new random element every time and return it. The below approaches can be used to accomplish this task: Table of Content Using Math.random() functionUsing splice() metho
2 min read
Randomly Select N Elements from List in Python When working with lists in Python, we often need to randomly select a specific number of elements. For example, consider the list a = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]. We might want to randomly select 3 elements from this list. Let's discuss different ways for selecting n random elements fr
3 min read
Select Random Element from Set in Python Selecting a random element from a group of samples is a deterministic task. A computer program can mimic such a simulation of random choices by using a pseudo-random generator. In this article, we will learn how to select a random element from a set in Python. What is a Set in Python?A Set is an uno
3 min read