How to generate and plot random numbers from a normal (Gaussian) distribution using python and matplotlib ?


Introduction

To generate random numbers from a normal (Gaussian) distribution in Python, you can use the random module or the numpy library. Below are examples demonstrating both methods.

Utilizing the numpy random module

The random module in numpy is particularly useful when working with large datasets, as it allows you to generate random numbers and arrays quickly and accurately for testing purposes.

Generate random numbers from a standard normal distribution

To generate random numbers from a standard normal distribution with a mean of 0 and a standard deviation of 1, use the following method:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(100000)

hx, hy, _ = plt.hist(data, bins=50, density=1,color="lightblue")

plt.ylim(0.0,max(hx)+0.05)
plt.title('Generate random numbers \n from a standard normal distribution with python')
plt.grid()

plt.savefig("numpy_random_numbers_stantard_normal_distribution.png", bbox_inches='tight')
plt.show()

The above code will generate 100,000 random numbers from a standard normal distribution with a mean of 0 and a standard deviation of 1. The resulting histogram plot shows the bell-shaped curve that is characteristic of a normal distribution.

How to generate and plot random numbers from a normal (Gaussian) distribution using python and matplotlib ?
How to generate and plot random numbers from a normal (Gaussian) distribution using python and matplotlib ?

Generate random numbers from a normal distribution

Generating random numbers from a standard normal distribution equips us with the ability to generate random numbers from any normal distribution. This can be accomplished using the formula $$X = Z * \sigma + \mu$$ where Z represents random numbers from a standard normal distribution, $\sigma$ denotes the standard deviation, and $\mu$ signifies the mean.

import numpy as np
import matplotlib.pyplot as plt

mu = 10.0
sigma = 2.0

data = np.random.randn(100000) * sigma + mu

hx, hy, _ = plt.hist(data, bins=50, density=1,color="lightblue")

plt.ylim(0.0,max(hx)+0.05)
plt.title('Generate random numbers \n from a normal distribution with python')
plt.grid()

plt.savefig("numpy_random_numbers_normal_distribution.png", bbox_inches='tight')
plt.show()

The above code will generate 100,000 random numbers from a normal distribution with a mean of 10 and a standard deviation of 2. The resulting histogram plot shows that the data is more concentrated around the mean compared to the previous example which had a standard deviation of 1.

How to generate and plot random numbers from a normal (Gaussian) distribution using python and matplotlib ?
How to generate and plot random numbers from a normal (Gaussian) distribution using python and matplotlib ?

Using the gauss function provided by the Python random module

An alternative approach is to utilize the gauss function provided by the Python random module

from random import gauss

gauss(100,15)

For instance, here is a randomly generated number as an example:

82.13399330852374

It is then possible to create a list of random numbers following a Gaussian distribution like this:

x = [gauss(100,15) for i in range(10000)]

and plot it using matplotlib:

from random import gauss

import matplotlib.pyplot as plt

x = [gauss(100,15) for i in range(10000)]

num_bins = 50
n, bins, patches = plt.hist(x, num_bins, density=1, facecolor='green', alpha=0.5)

plt.show()

Additionally, the output of this code can be saved as an image or further manipulated using matplotlib's extensive features:

How to generate and plot random numbers from a normal (Gaussian) distribution using python and matplotlib ?
How to generate and plot random numbers from a normal (Gaussian) distribution using python and matplotlib ?

This allows for further analysis and visualization of data following a Gaussian distribution. Other distributions such as Poisson, Binomial, and Exponential can also be simulated and plotted in a similar manner.

References

Links Site
Normal distribution en.wikipedia.org
numpy.random.randn docs.scipy.org
numpy.random.normal docs.scipy.org
Image

of