AppDividend
Latest Code Tutorials

np.random.normal: How to use numpy random normal

The np.random.normal() is a numpy function used to get random numbers for the normal distribution. The normal distribution is often called the bell curve because of its shape. As the shape of the normal distribution graph looks like the bell, it is often called the bell curve.

Syntax

random.normal(loc = 0.0, scale =1.0, size = None)

Arguments

loc: It is also called the Mean. It is the mean of the normal distribution. The mean is the average of the normal distribution. If nothing is passed in the argument, it automatically assumes 0.0 as the mean for the normal distribution. Because if the normal distribution is obtained, then the mean is 0; hence 0.0 is assigned by default. 

scale: It is also called the standard deviation. It is the standard deviation of the normal distribution. As the name suggests it is the standard deviations between the points in the normal distribution. If the standard deviation is passed as the argument, the function automatically assigns the value as 1.0. 

size: It is the number of samples to be drawn. If n is the number of samples passed in the argument, then n samples are drawn. If nothing is given in the argument then 1 sample will be drawn.

Python program for np.random.normal()

import numpy as np

normal_dist = np.random.normal()
print(normal_dist)

Output

3.5463349551368286

In this program, we imported a numpy package consisting of several functions. We used a function called random.normal(). This is the function present inside the random class of the numpy package.

The np.random.normal() function finds the normal distribution for the random samples.

In this program, we have not passed anything np.random.normal() function; hence, it automatically assumes the mean as 0, the standard variance as 1, and the sample size as None. Therefore if we run this program, we will get a random normal distribution value. We will get a single sample as we have not mentioned the size. If None is given, it returns one sample.

Python program for using np.random.normal() with a mean and standard deviation

import numpy as np

# creating a variable for storing mean
mu = 1.0

# creating a variable for storing standard deviation
sigma = 2.5

# creating a variable for storing the number of samples
siz = 7
normal_dist = np.random.normal(mu, sigma, siz)
print(normal_dist)

Output

[-1.58196785 0.29736585 1.81180083 4.33999648 
 5.55233104 2.37637558 -1.43250784]

In this program, we created three variables named mu, sigma, and siz. The mu is used for storing the mean of the normal distribution. Sigma is used for storing the standard deviation. And the siz is used for storing the number of samples.

We passed the mean, standard deviation, and size to the function. Hence, the output will have seven samples as we have given seven as the size.

That’s it for this tutorial.

Related posts

np.random.randint

np.random.rand

np.random.permutation

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.