normal(loc, scale, size) is the more explicit, general form of randn(): loc sets the distribution's mean, default 0, scale sets its standard deviation, default 1, and size controls the shape of the output array. It's the standard tool for simulating naturally-distributed real-world quantities — measurement noise, heights, test scores — anything whose values cluster around a typical value with a symmetric bell-curve spread.
1Understanding np.random.normal()
normal(loc, scale, size) is the more explicit, general form of randn(): loc sets the distribution's mean, default 0, scale sets its standard deviation, default 1, and size controls the shape of the output array. It's the standard tool for simulating naturally-distributed real-world quantities — measurement noise, heights, test scores — anything whose values cluster around a typical value with a symmetric bell-curve spread.
Prefer np.random.normal(mean, std, size) over manually scaling and shifting randn()'s output — it's more directly readable and explicit about the specific distribution parameters being used.
import numpy as np
np.random.seed(0)
samples = np.random.normal(loc=100, scale=15, size=5)
print(np.round(samples, 2))2Practical Example
Here is a real-world application of np.random.normal() showing how it is used in production NumPy code.
import numpy as np
np.random.seed(0)
heights = np.random.normal(170, 10, 1000)
print(round(heights.mean(), 1))
print(round(heights.std(), 1))3Best Practices
Follow these guidelines when working with np.random.normal():
1. Use np.random.normal(mean, std, size) directly instead of manually scaling and shifting randn()'s output, for clearer, more self-documenting code
2. Set scale, standard deviation, thoughtfully based on the real-world variability you're simulating, not an arbitrary default
3. Seed explicitly for reproducible test data or examples relying on normal()'s output
Tip: Prefer np.random.normal(mean, std, size) over manually scaling and shifting randn()'s output — it's more directly readable and explicit about the specific distribution parameters being used.
import numpy as np
np.random.seed(0)
samples = np.random.normal(loc=100, scale=15, size=5)
print(np.round(samples, 2))