solution_198.py

#!/usr/bin/python3
# ====================================================================
# create a population of random numbers from a theoretical
# normal distribution (bell curve) - display various values and plots
#
# if you do not run as admin (root), install defaults to user
# installation because normal site-packages is not writeable
#    python -m pip install matplotlib
#    python -m pip install scipy
# ====================================================================

import random
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt

mean               = 0.0
number_of_bins     = 20
population_size    = 1000
standard_deviation = 1.0

# ---- create a list of numbers that fit
# ---- a standard distributed (bell curve)
# ---- (normal/gauss distribution)

pop = []

for _ in range(1000):
    pop.append(random.gauss(mean,standard_deviation))

print(f'pop size = {len(pop)}')

# ---- population min/max values

pmin = min(pop)
pmax = max(pop)

print(f'pop min  = {pmin}')
print(f'pop max  = {pmax}')

# ---- plot histogram

counts,bins,ignore = plt.hist(pop,bins=number_of_bins)

# ---- plot theoretical probability distribution

bin_width = (pmax-pmin)/number_of_bins
hist_area = len(pop)*bin_width

x = np.linspace(-4,4,number_of_bins+1)
y = scipy.stats.norm.pdf(x)*hist_area
plt.plot(x,y)

# ---- display plots

plt.show()