0
0
NumPydata~5 mins

Random choice from array in NumPy

Choose your learning style9 modes available
Introduction

We use random choice to pick one or more items from a list or array without any specific order. This helps when we want to simulate random events or select samples.

Choosing a random winner from a list of participants in a contest.
Selecting random samples from a dataset for testing or training.
Simulating dice rolls or card draws in games.
Randomly picking a product to recommend to a user.
Testing algorithms with random inputs.
Syntax
NumPy
import numpy as np

# To pick one random element
random_element = np.random.choice(array)

# To pick multiple random elements with replacement
random_elements = np.random.choice(array, size=number_of_elements, replace=True)

# To pick multiple random elements without replacement
random_elements_no_repeat = np.random.choice(array, size=number_of_elements, replace=False)

The array can be a list or a numpy array.

replace=True means the same element can be picked more than once.

Examples
Randomly picks one element from the list.
NumPy
import numpy as np

array = [10, 20, 30, 40, 50]

# Pick one random element
random_element = np.random.choice(array)
print(random_element)
Randomly picks three elements, elements can repeat.
NumPy
import numpy as np

array = [10, 20, 30, 40, 50]

# Pick three elements with replacement
random_elements = np.random.choice(array, size=3, replace=True)
print(random_elements)
Randomly picks three unique elements, no repeats.
NumPy
import numpy as np

array = [10, 20, 30, 40, 50]

# Pick three elements without replacement
random_elements_no_repeat = np.random.choice(array, size=3, replace=False)
print(random_elements_no_repeat)
Shows error when trying to pick from an empty array.
NumPy
import numpy as np

array = []

# What if array is empty?
try:
    random_element = np.random.choice(array)
except ValueError as error:
    print(f"Error: {error}")
Sample Program

This program shows how to pick random elements from an array in different ways using numpy.

NumPy
import numpy as np

# Create an array of numbers
numbers = [5, 10, 15, 20, 25]

print("Original array:", numbers)

# Pick one random element
random_one = np.random.choice(numbers)
print("Randomly picked one element:", random_one)

# Pick three elements with replacement
random_three_with_replace = np.random.choice(numbers, size=3, replace=True)
print("Randomly picked three elements with replacement:", random_three_with_replace)

# Pick three elements without replacement
random_three_no_replace = np.random.choice(numbers, size=3, replace=False)
print("Randomly picked three elements without replacement:", random_three_no_replace)
OutputSuccess
Important Notes

Time complexity is O(n) where n is the number of elements to pick.

Space complexity is O(n) for the output array of chosen elements.

Common mistake: Trying to pick more elements than exist without replacement causes an error.

Use replace=False when you want unique picks, replace=True when repeats are allowed.

Summary

Random choice helps pick one or more items randomly from an array.

You can pick with or without replacement depending on whether repeats are allowed.

Always check array size when picking without replacement to avoid errors.