Active February 05, 2019    /    Viewed 23415    /    Comments 0    /    Edit

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()


### References

##### Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!

Did you find this content useful ?, If so, please consider donating a tip to the author(s). MoonBooks.org is visited by millions of people each year and it will help us to maintain our servers and create new contents.

Amount