🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.random.choice()

AI & DATA SCIENCE // np-random-choice

np.random.choice() generates a random sample from a given 1D array or range, optionally with replacement, and optionally with custom per-element probabilities.

Syntax

np.random.choice(a, size=None, replace=True, p=None)

Deep Dive Course

Passing an integer for a is shorthand for sampling from a range up to that integer, a common convenience for picking random indices. By default, replace=True means the same element can be selected more than once, even within a single call requesting multiple samples; setting replace=False instead samples without replacement, guaranteeing every selected element is distinct, similar to drawing cards from a deck without putting them back. The p parameter lets you assign a custom probability to each element instead of assuming a uniform distribution over all of them.

1Understanding np.random.choice()

Passing an integer for a is shorthand for sampling from a range up to that integer, a common convenience for picking random indices. By default, replace=True means the same element can be selected more than once, even within a single call requesting multiple samples; setting replace=False instead samples without replacement, guaranteeing every selected element is distinct, similar to drawing cards from a deck without putting them back. The p parameter lets you assign a custom probability to each element instead of assuming a uniform distribution over all of them.

💡

Set replace=False specifically when you need a random subset with no duplicates, like randomly selecting several distinct items from a list — the default, replace=True, can select the very same element more than once.

editor.html
import numpy as np

np.random.seed(0)
colors = np.array(["red", "green", "blue", "yellow"])
print(np.random.choice(colors, size=3))
localhost:3000

2Practical Example

Here is a real-world application of np.random.choice() showing how it is used in production NumPy code.

editor.html
import numpy as np

np.random.seed(0)
weighted = np.random.choice(["heads", "tails"], size=5, p=[0.9, 0.1])
print(weighted)
localhost:3000

3Best Practices

Follow these guidelines when working with np.random.choice():

1. Set replace=False when sampling should never select the same element twice, like choosing a subset of distinct items

2. Use the p parameter to draw from a weighted/non-uniform distribution instead of manually implementing weighted sampling

3. Pass an integer for a as a convenient shorthand for sampling random indices from a range, instead of building the range array explicitly

⚠️

Tip: Set replace=False specifically when you need a random subset with no duplicates, like randomly selecting several distinct items from a list — the default, replace=True, can select the very same element more than once.

editor.html
import numpy as np

np.random.seed(0)
colors = np.array(["red", "green", "blue", "yellow"])
print(np.random.choice(colors, size=3))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

np.random.seed(0)
colors = np.array(["red", "green", "blue", "yellow"])
print(np.random.choice(colors, size=3))
Example 02Advanced Example
import numpy as np

np.random.seed(0)
weighted = np.random.choice(["heads", "tails"], size=5, p=[0.9, 0.1])
print(weighted)

Best Practices

  • Set replace=False when sampling should never select the same element twice, like choosing a subset of distinct items
  • Use the p parameter to draw from a weighted/non-uniform distribution instead of manually implementing weighted sampling
  • Pass an integer for a as a convenient shorthand for sampling random indices from a range, instead of building the range array explicitly

Interview Question

What's the difference between calling np.random.choice() with replace=True (the default) versus replace=False?

Hint: Think about whether the same element can be picked more than once.

With the default replace=True, each selection is made independently from the full original set of options, so the same element can legitimately be chosen multiple times within a single call, the same way rolling a die multiple times can produce the same number repeatedly. With replace=False, once an element has been selected, it's removed from the pool of remaining candidates for subsequent selections, guaranteeing every element in the result is distinct — which also means size can't exceed the number of available elements when replace=False, unlike the unlimited resampling replace=True allows.

Exercises

MediumPractice using np.random.choice() in a real scenario.
View Solution
import numpy as np

np.random.seed(0)
colors = np.array(["red", "green", "blue", "yellow"])
print(np.random.choice(colors, size=3))

Frequently Asked Questions

What's the difference between calling np.random.choice() with replace=True (the default) versus replace=False?

With the default replace=True, each selection is made independently from the full original set of options, so the same element can legitimately be chosen multiple times within a single call, the same way rolling a die multiple times can produce the same number repeatedly. With replace=False, once an element has been selected, it's removed from the pool of remaining candidates for subsequent selections, guaranteeing every element in the result is distinct — which also means size can't exceed the number of available elements when replace=False, unlike the unlimited resampling replace=True allows.

Related Functions

np-random-shufflenp-random-permutationnp-random-seed