# How to generate random numbers from a normal (Gaussian) distribution in python ?

Examples of how to generate random numbers from a normal (Gaussian) distribution in python:

### Generate random numbers from a standard normal (Gaussian) distribution

To generate a random numbers from a standard normal distribution ($\mu_0=0$ , $\sigma=1$)

import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(100000)

hx, hy, _ = plt.hist(data, bins=50, normed=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()


### Generate random numbers from a normal (Gaussian) distribution

If we know how to generate random numbers from a standard normal distribution, it is possible to generate random numbers from any normal distribution with the formula $$X = Z * \sigma + \mu$$ where Z is random numbers from a standard normal distribution, $\sigma$ the standard deviation $\mu$ 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, normed=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()


Image

of