How to randomly select elements of an array with NumPy in Python ? Last Updated : 28 May, 2025 Comments Improve Suggest changes Like Article Like Report 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) Output[2 5 4] 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) Output[1 4 2] 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) Output[1 2 5] 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=TrueThis 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) Output[5 2 1] 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 articlesnumpy.random.choice()numpy.random.shuffle()numpy.random.permutation() Comment More infoAdvertise with us Next Article How to randomly select elements of an array with NumPy in Python ? mprerna802 Follow Improve Article Tags : Python Python-numpy Python numpy-Random Practice Tags : python 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 NumPy| How to get the unique elements of an Array To find unique elements of an array we use the numpy.unique() method of the NumPy library in Python. It returns unique elements in a new sorted array. Example: Python3 import numpy as np arr = np.array([1, 2, 3, 1, 4, 5, 2, 5]) unique_elements = np.unique(arr) print(unique_elements) Output: [1 2 3 4 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 Randomly select elements from list without repetition in Python We are given a list, and our task is to randomly select elements from it without repetition. This means that once an element is selected, it should not be picked again. For example, given a = [10, 20, 30, 40, 50], selecting three elements randomly could result in [30, 10, 50], but the same element s 2 min read How to randomly insert NaN in a matrix with NumPy in Python ? Prerequisites: Numpy In this article, let's see how to generate a Python Script that randomly inserts Nan into a matrix using Numpy. Given below are 3 methods to do the same: Method 1: Using ravel() function ravel() function returns contiguous flattened array(1D array with all the input-array elemen 3 min read How to remove specific elements from a NumPy array ? In this article, we will discuss how to remove specific elements from the NumPy Array. Remove specific elements from a NumPy 1D arrayDeleting element from NumPy array using np.delete() The delete(array_name ) method will be used to do the same. Where array_name is the name of the array to be delete 3 min read Select an element or sub array by index from a Numpy Array The elements of a NumPy array are indexed just like normal arrays. The index of the first element will be 0 and the last element will be indexed n-1, where n is the total number of elements. Selecting a single element from a NumPy array Each element of these ndarrays can be accessed using its index 2 min read Like