🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.random.normal()

AI & DATA SCIENCE // np-random-normal

np.random.normal() draws random samples from a normal (Gaussian) distribution with a specified mean and standard deviation.

Syntax

np.random.normal(loc=0.0, scale=1.0, size=None)

Deep Dive Course

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.

editor.html
import numpy as np

np.random.seed(0)
samples = np.random.normal(loc=100, scale=15, size=5)
print(np.round(samples, 2))
localhost:3000

2Practical Example

Here is a real-world application of np.random.normal() showing how it is used in production NumPy code.

editor.html
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))
localhost:3000

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.

editor.html
import numpy as np

np.random.seed(0)
samples = np.random.normal(loc=100, scale=15, size=5)
print(np.round(samples, 2))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

np.random.seed(0)
samples = np.random.normal(loc=100, scale=15, size=5)
print(np.round(samples, 2))
Example 02Advanced Example
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))

Best Practices

  • Use np.random.normal(mean, std, size) directly instead of manually scaling and shifting randn()'s output, for clearer, more self-documenting code
  • Set scale, standard deviation, thoughtfully based on the real-world variability you're simulating, not an arbitrary default
  • Seed explicitly for reproducible test data or examples relying on normal()'s output

Interview Question

What's the relationship between np.random.normal() and np.random.randn()?

Hint: Think about what randn() specifically produces and how normal() generalizes it.

randn() always samples from the standard normal distribution specifically, mean 0 and standard deviation 1, with no way to directly specify different parameters. normal(loc, scale, size) generalizes that by explicitly accepting the desired mean and standard deviation, internally applying the same scale-and-shift transformation that you'd otherwise have to do manually on randn()'s output. For the default arguments, loc=0 and scale=1, normal() and randn() draw from exactly the same distribution.

Exercises

MediumPractice using np.random.normal() in a real scenario.
View Solution
import numpy as np

np.random.seed(0)
samples = np.random.normal(loc=100, scale=15, size=5)
print(np.round(samples, 2))

Frequently Asked Questions

What's the relationship between np.random.normal() and np.random.randn()?

randn() always samples from the standard normal distribution specifically, mean 0 and standard deviation 1, with no way to directly specify different parameters. normal(loc, scale, size) generalizes that by explicitly accepting the desired mean and standard deviation, internally applying the same scale-and-shift transformation that you'd otherwise have to do manually on randn()'s output. For the default arguments, loc=0 and scale=1, normal() and randn() draw from exactly the same distribution.

Related Functions

np-random-randnnp-random-uniformnp-std