What is random choice() using NumPy in Python?

Overview

The random module in Python comes with various methods that return randomly generated data distribution.

  • Data distribution is a function or list that shows all the possible values or intervals of the data.

  • A random distribution is a collection of random numbers that obeys a particular probability density function.

  • The probability density function is the probability of all the values in an array.

Generating random numbers based on defined probabilities

The random module in NumPy generates random numbers based on a defined probability density function. It uses the choice(() function to do so.

The choice() function

The choice() function specifies the probability for each array value and generates a 1D array.

Syntax

choice(list, p=None, size=None)

Parameter value

The choice() function takes the following parameter values:

  • list: This represents the 1D array with the random values.
  • p: This represents the probability for each array value. They all must sum up to 1.
  • size: This represents the size of the output 1D array of the random values.

Code

from numpy import random
# generating a random distribution with a defined probabilty density function
myarray = random.choice([1, 2, 3, 4, 5], p = [0.1, 0.3, 0.5, 0.1, 0.0], size = 50)
print(myarray)

Explanation

  • Line 1: We import the random module from NumPy.

  • Line 4: We generate a random distribution, using the choice() function. We define a probability density function for all the values of the array. This is as follows:

    1. The probability of 1 being returned in the distribution is 0.1.
    2. The probability of 2 being returned in the distribution is 0.3.
    3. The probability of 3 being returned in the distribution is 0.5.
    4. The probability of 4 being returned in the distribution is 0.1.
    5. The probability of 5 being returned in the distribution is 0.0.

    We give the size of the output 1D array to be 50.

  • Line 7: We print the myarray array.

Note: No matter how many times we rerun that code, it never returns the value 5. This is because we already assigned its probability as 0.0.

Free Resources