لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

# Python Data Science NumPy Random Data Distribution

The random module offer methods that returns randomly generated data distributions.

As we know that Data Distribution is a list of all possible values, and how often each value occurs.

These lists are important while working with statistics and data sciences.

A Random Distribution

A random distribution is a set of random numbers that follow a certain probability density function.

What is Probability Density Function?

A Probability Density Function is a function that describes a continuous probability. i.e. probability of all values in an array.

Random numbers can be generated based on defined probabilities using the choice() method of the random module.

The choice() method allows us to specify the probability for each value.

The probability is set by a number between 0 and 1, where 0 means that the value will never occur and 1 means that the value will always occur.

Example 1: Generate a 1-D array containing 200 values, where each value has to be 2, 4, 6 or 8.

The probability for the value to be 2 is set to be 0.2

The probability for the value to be 4 is set to be 0.3

The probability for the value to be 6 is set to be 0.5

The probability for the value to be 8 is set to be 0

Code

from numpy import random

x = random.choice([2, 4, 6, 8], p=[0.2, 0.3, 0.5, 0.0], size=(200))

print(x)

the output will be

[2 2 6 4 2 4 4 6 6 2 4 4 4 2 6 2 6 6 4 4 2 6 2 4 2 4 6 2 6 4 6 6 6 6 4 6 6

4 6 6 4 2 4 2 6 4 6 2 6 2 2 6 2 4 6 6 2 4 4 6 4 4 6 6 4 6 6 4 6 4 6 4 4 2

4 6 4 2 6 4 4 4 4 2 6 6 6 6 4 6 6 6 4 4 4 2 6 2 6 6 4 6 6 6 6 4 4 6 2 6 4

2 2 2 4 6 2 6 2 4 6 2 2 2 6 6 6 6 4 6 2 6 6 4 4 4 6 4 6 6 6 4 2 6 4 4 4 4

4 6 4 6 4 6 6 2 6 6 6 2 6 4 6 6 4 2 2 6 6 4 4 4 4 6 4 2 4 2 6 6 6 4 4 2 2

2 2 2 2 4 2 2 6 2 2 6 4 4 6 6]

Note: Each time the code is run the output will be different as number are generated randomly.
The sum of all probability numbers should be 1.
Even if you run the example above 1000 times, the value 8 will never occur because the probability of 8 ocuuring is set to 0.

How to return arrays of any shape and size by specifying the shape in the size parameter?

Example 2: In the above example return a 2-D array with 3 rows, each containing 5 values.

Code

from numpy import random

x = random.choice([2, 4, 6, 8], p=[0.2, 0.3, 0.5, 0.0], size=(3, 5))

print(x)

the output will be

```[[2 6 4 6 2]
[4 6 6 6 6]
[6 2 6 6 2]]
```