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.
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 specifies the probability for each array value and generates a 1D
array.
choice(list, p=None, size=None)
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.from numpy import random# generating a random distribution with a defined probabilty density functionmyarray = random.choice([1, 2, 3, 4, 5], p = [0.1, 0.3, 0.5, 0.1, 0.0], size = 50)print(myarray)
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
being returned in the distribution is 0.1
.2
being returned in the distribution is 0.3
.3
being returned in the distribution is 0.5
.4
being returned in the distribution is 0.1
.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 as0.0
.