Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.random.choice

Generates a random sample from a one-dimensional array-like population or from the range np.arange(a). You can control output shape, replacement, and per-item probabilities.

Since
1.7.0

How it works

The function draws random values from a one-dimensional array-like input, or from the integer range starting at zero when given an integer. It returns one value by default or an array shaped by size; replace controls reuse, while p optionally assigns per-entry probabilities.

Syntax

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

Parameters

NameTypeDescription
a1-D array-like or intOne-dimensional array-like population, or an integer interpreted as np.arange(a).
sizeint or tuple of ints, optionalOutput shape. An integer requests that many samples; a tuple requests that shaped output.
replaceboolean, optionalWhether sampling allows an item to be selected repeatedly. Defaults to True.
p1-D array-like, optionalProbabilities associated with the entries in a; omitted means uniform sampling.

Returns

A scalar when size is None; otherwise an ndarray containing the requested sample shape, with values selected from a.

Key points

  • Use the function to select random elements from a one-dimensional array-like population.
  • Leaving size unset produces a scalar selection instead of an array.
  • The default sampling mode is with replacement, so duplicate selections are allowed.
  • Without p, every population entry has equal selection probability.
  • For new programs, prefer calling choice on a Generator rather than using this legacy convenience function.

Common mistakes

  • When p is supplied, it must describe probabilities for every entry in a; otherwise the distribution is uniform.
  • Setting replace=False prevents an item from being selected more than once, so size cannot exceed the population.
  • An integer a represents the half-open range from zero through a - 1, rather than a literal one-element population.

Example: Weighted sampling with replacement

import numpy as np

rng = np.random.default_rng(42)
items = np.array(["red", "green", "blue"])
sample = rng.choice(items, size=5, replace=True, p=[0.6, 0.3, 0.1])
print(sample)

This runnable example uses the recommended Generator method to draw five weighted samples, allowing repeated selections.

When to use it

Random element selection

Selecting random labels, IDs, or categories from a finite one-dimensional collection.

Sampling with replacement

Creating bootstrap-style samples where observations may recur.

Weighted sampling

Drawing outcomes with explicitly different likelihoods.

Sampling without replacement

Producing a fixed-size subset with unique entries.

Interview questions

QWhat does np.random.choice accept and return?

It samples from a one-dimensional array-like input, or treats an integer a as np.arange(a). With the default size=None it returns one value; a size integer or tuple determines the output shape.

Follow-up: How would you sample rows from a multidimensional array?

QHow does replacement affect sampling?

replace=True is the default and permits repeated values. With replace=False, each selected item is removed from consideration, so the requested sample must fit within the available population.

Follow-up: What changes when weighted probabilities are also supplied?

QHow do you perform weighted sampling?

p supplies one probability per element of a. Omitting p gives a uniform distribution, while supplying it creates a non-uniform distribution according to those per-entry probabilities.

Follow-up: When should new code use another API?

  • numpy.random.Generator.choice
  • numpy.random.randint
  • numpy.arange

Source: numpy.random.choice — NumPy v2.5 Manual. Checked 2026-10-15.