What is the random.normal() function in NumPy?

The random.normal() function is part of Python’s NumPy library and is used to model probabilistic experiments with the help of normal distribution.

To use the random.normal function, you must import numpy at the very start of the program:

import numpy

Syntax

The random.normal method has the following syntax:

numpy.random.normal(m,s,n)

Parameters

The random.normal function takes in three parameters:

  • m: the mean of the normal distribution.
  • s: the standard deviation of the distribution.
  • n: the total number of samples to be drawn.

In any kind of normally distributed data, the majority of the data points are clustered around the mean. The normal distribution is symmetrical and can be graphed as a bell curve.

Return value

The random.normal method returns a list of size n, which is the number of samples. In other words, the return value is the outcome of drawing out 2000 random samples from normally distributed data that has a mean and standard deviation of m and s, respectively.

Code

The code below shows how to use the random.normal function in Python.

First, we set the values of m, s, and n to 6.7, 1.2, and 2000, respectively. This is to denote that 2000 samples will be drawn out from normally distributed data with a mean of 6.7 and a standard deviation of 1.2.

Next, we call the random.normal function and print the return value, its mean, and its size with the print function. The return value is a list of 2000 values, most of which are clustered around the mean. Note that the mean is almost equal to the value of m.

The total area under a normal curve is equal to one; this is because the area under a normal curve represents the probability of a randomly drawn sample having a certain value.

#import numpy library at the start of the program
import numpy as np
#assign a value to the mean of the normal distribution
m=6.7
#assign a value to the standard deviation of the distribution
s=1.2
#apply the pmf on a normally distributed data of 2000 datapoints
#and m and s as its mean and s.d, respectively
ret = np.random.normal(m, s, 2000)
#the return value has 2000 values, which is also
#equal to n
print(len(ret), ret.mean(), ret)

If we plot the return value, it would look like a bell curve as shown below:

Bell curve of 2000 normally distributed samples with mean=6.7 and s.d=1.2

As illustrated in the bell curve, more than 250 samples are clustered around the mean, which is located at the center of the distribution.

Free Resources