NumPy Normal Distribution
The normal (Gaussian) distribution is the classic bell curve, and np.random.normal lets you sample from it by specifying a center, a spread, and how many values you want.
What Is the Normal Distribution?
The normal distribution is a symmetric, bell-shaped curve where values near the center are common and values far from the center become increasingly rare. Many real-world measurements approximate it, including heights, test scores, and measurement error, largely because of the central limit theorem: when many small independent effects add together, their sum tends toward a normal shape regardless of the shape of the individual effects.
Example
import numpy as np
np.random.seed(10)
# 10 samples from a standard normal distribution (mean 0, std 1)
samples = np.random.normal(loc=0, scale=1, size=10)
print(samples)Understanding loc, scale, and size
np.random.normal takes three arguments. loc sets the center of the curve, which is the mean of the distribution. scale sets the spread, which is the standard deviation - a larger scale produces values that land farther from loc more often. size controls the shape of the output: an integer gives a 1D array of that length, a tuple gives a multi-dimensional array, and leaving size out entirely returns a single float. Both loc and scale default to 0.0 and 1.0 when omitted.
Example
import numpy as np
np.random.seed(11)
# Simulated adult heights in cm: centered at 170, spread of 7
heights = np.random.normal(loc=170, scale=7, size=(2, 3))
print(heights)
# A larger scale spreads values farther from the center
narrow = np.random.normal(loc=0, scale=1, size=5)
wide = np.random.normal(loc=0, scale=10, size=5)
print("Narrow spread:", narrow)
print("Wide spread: ", wide)Checking Samples Against loc and scale
Since np.random.normal just returns a NumPy array, you can immediately run np.mean and np.std on it to see how closely the sample matches the parameters you asked for. With only a few samples the match will be rough, but as size grows the sample mean and sample standard deviation converge toward loc and scale - a consequence of the law of large numbers.
- Modeling measurement noise or error added to a clean signal.
- Generating synthetic test data for physical measurements like height or weight.
- Approximating financial returns over short time horizons.
- Initializing neural network weights before training begins.
- Simulating natural variation for statistics teaching examples.
Example
import numpy as np
np.random.seed(12)
# Large sample: the sample stats should land close to loc and scale
large_sample = np.random.normal(loc=50, scale=5, size=10000)
print("Requested loc=50, scale=5")
print("Sample mean:", round(np.mean(large_sample), 2))
print("Sample std: ", round(np.std(large_sample), 2))Exercise: NumPy Normal Distribution
Which two main parameters define a distribution in np.random.normal()?