VOOZH about

URL: https://www.geeksforgeeks.org/python/numpy-random-choice-in-python/

⇱ numpy.random.choice() in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

numpy.random.choice() in Python

Last Updated : 22 Aug, 2025

numpy.random.choice() function allows you to randomly select elements from an array. It’s a part of NumPy's random module and is widely used for sampling with or without replacement, shuffling data, simulations and bootstrapping.

Example:


Output
40

Explanation: A single random element is selected from the array. In this case, 40 was picked randomly (your output may vary since it's random).

Syntax

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

Parameters:

  • a: 1D array-like or int. If int n, samples from np.arange(n).
  • size: Number of samples (int or tuple). Default is one value.
  • replace: If True, samples with replacement. Default is True.
  • p: List of probabilities associated with a. Must sum to 1.

Returns: A single value or an array of values based on sampling rules.

Examples

1. Pick one value


Output
3

2. Pick multiple values (with replacement)


Output
[30 30]

3. Sample without replacement


Output
['a' 'c']

4. Use custom probabilities


Output
['green' 'green' 'green']

6. Multi-dimensional output


Output
[[1 1 1]
 [0 0 0]]
Comment
Article Tags: