Generate Normally Distributed Data in Python

Your goal

You need to randomly generate normally distributed data in Python.

Step-by-step tutorial

We will use the normal function from numpy.random.

A normal distribution has two parameters: the mean and standard deviation. Let's generate 100 adult male heights with mean \(\mu = 70\) inches and standard deviation \(\sigma = 3\) inches.

>>> import numpy as np
>>> data = np.random.normal(loc=70, scale=3, size=100)
>>> data
array([74.29297096, 73.12992281, 74.91977509, 68.80277479, 69.80631598,
       70.47453008, 70.92029277, 69.06615691, 66.18512427, 75.69575112,
       74.24178522, 64.61090449, 71.36984919, 63.54751549, 72.15935968,
       70.88489537, 67.00578531, 66.15792488, 73.27493731, 64.14543555,
       75.21697645, 70.89835642, 65.93237463, 68.35653624, 71.06187514,
       75.77429938, 72.57773296, 68.59966913, 67.56968526, 75.69141416,
       66.58209055, 72.26112094, 71.85172533, 69.63853659, 71.96602069,
       71.19398797, 67.16904573, 69.08607761, 69.91377253, 69.12423451,
       63.48152232, 71.76803403, 68.43869577, 72.51793594, 68.91990128,
       68.97269834, 70.9385555 , 66.38315249, 67.11880376, 70.96519026,
       64.68069696, 67.05468634, 69.30011549, 67.54172967, 68.58511967,
       66.72166756, 69.26120503, 73.64866641, 72.25028203, 71.15624954,
       73.16108855, 63.79194664, 72.78265143, 70.73548139, 73.02705619,
       71.96123469, 70.97874172, 72.17881023, 73.16366823, 74.27951255,
       65.78696148, 71.22996071, 65.63255966, 70.58546921, 73.42248602,
       68.07421577, 72.72106249, 72.0100412 , 77.59010214, 73.48192355,
       73.3919707 , 69.20880649, 70.97752453, 67.31540036, 71.68060339,
       68.54231257, 72.97953403, 67.61536422, 69.36021524, 72.51068909,
       68.31782024, 73.35154963, 68.14039711, 72.70162301, 73.85444734,
       63.03892014, 70.60164205, 75.41860425, 64.31834854, 69.53105485])

Here's a histogram for the data:

Histogram of normally distributed data
Histogram of normally distributed data