random.normalvariate

Full name
random.normalvariate
Library
random
Syntax

random.normalvariate(mu, sigma)

Description

The random.normalvariate function returns a random number drawn from a normal distribution.

Parameters
  • mu: Mean of the distribution.
  • sigma: Standard deviation of the distribution.
Result

The random.normalvariate function returns a real number.

Examples

We can generate a random number extracted from a normal distribution with mean 5 and standard deviation 3 with the following code:

random.normalvariate(5, 3)
0.7129299750420737

To confirm the distribution from which the random numbers are extracted we can generate one hundred thousand random numbers from a normal distribution of mean 5 and standard deviation 3 and show its histogram:

import matplotlib.pyplot as plt
plt.figure(figsize = (8, 4))
plt.hist([random.normalvariate(5, 3) for i in range(100000)], bins = 100)
plt.grid()
plt.show()
random.normalvariate
Submitted by admin on Wed, 03/17/2021 - 18:11