🚀 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.randn()

AI & DATA SCIENCE // np-random-randn

np.random.randn() generates an array of a given shape filled with random samples from the standard normal distribution (mean 0, standard deviation 1).

Syntax

np.random.randn(d0, d1, ...)

Deep Dive Course

Like rand(), randn() takes each dimension as a separate positional argument rather than a shape tuple, but unlike rand(), it draws from a Gaussian, bell curve, distribution centered at 0 with a standard deviation of 1, rather than a uniform distribution — most values cluster near 0, and values further away become progressively less likely, following the classic bell-curve shape. To get samples from a normal distribution with a different mean and standard deviation, scale and shift the result manually, or use np.random.normal() directly, which accepts those parameters explicitly.

1Understanding np.random.randn()

Like rand(), randn() takes each dimension as a separate positional argument rather than a shape tuple, but unlike rand(), it draws from a Gaussian, bell curve, distribution centered at 0 with a standard deviation of 1, rather than a uniform distribution — most values cluster near 0, and values further away become progressively less likely, following the classic bell-curve shape. To get samples from a normal distribution with a different mean and standard deviation, scale and shift the result manually, or use np.random.normal() directly, which accepts those parameters explicitly.

💡

To sample from a normal distribution with a specific mean and standard deviation, either compute mean plus standard deviation times randn()'s output manually, or use the more explicit np.random.normal(mean, std, size) directly — the latter is usually clearer about intent.

editor.html
import numpy as np

np.random.seed(0)
print(np.random.randn(3))
localhost:3000

2Practical Example

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

editor.html
import numpy as np

np.random.seed(0)
mean, std = 100, 15
samples = mean + std * np.random.randn(3)
print(samples)
localhost:3000

3Best Practices

Follow these guidelines when working with np.random.randn():

1. Use np.random.normal(mean, std, size) instead of manually scaling randn()'s output when you need a specific mean/standard deviation, for clearer code

2. Set a seed for reproducibility in tests or demonstrations relying on randn()'s output

3. Prefer the newer Generator API (np.random.default_rng().standard_normal()) over the legacy randn() in new code

⚠️

Tip: To sample from a normal distribution with a specific mean and standard deviation, either compute mean plus standard deviation times randn()'s output manually, or use the more explicit np.random.normal(mean, std, size) directly — the latter is usually clearer about intent.

editor.html
import numpy as np

np.random.seed(0)
print(np.random.randn(3))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

np.random.seed(0)
print(np.random.randn(3))
Example 02Advanced Example
import numpy as np

np.random.seed(0)
mean, std = 100, 15
samples = mean + std * np.random.randn(3)
print(samples)

Best Practices

  • Use np.random.normal(mean, std, size) instead of manually scaling randn()'s output when you need a specific mean/standard deviation, for clearer code
  • Set a seed for reproducibility in tests or demonstrations relying on randn()'s output
  • Prefer the newer Generator API (np.random.default_rng().standard_normal()) over the legacy randn() in new code

Interview Question

How would you generate random samples from a normal distribution with a mean of 100 and a standard deviation of 15 using randn()?

Hint: Think about how scaling and shifting a standard normal distribution changes its parameters.

randn() always draws from the standard normal distribution, mean 0, standard deviation 1. To transform those samples into a distribution with a different mean and standard deviation, you multiply each sample by the desired standard deviation, which scales its spread, and then add the desired mean, which shifts its center — a standard technique for reparameterizing any normal distribution from the standard one.

Exercises

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

np.random.seed(0)
print(np.random.randn(3))

Frequently Asked Questions

How would you generate random samples from a normal distribution with a mean of 100 and a standard deviation of 15 using randn()?

randn() always draws from the standard normal distribution, mean 0, standard deviation 1. To transform those samples into a distribution with a different mean and standard deviation, you multiply each sample by the desired standard deviation, which scales its spread, and then add the desired mean, which shifts its center — a standard technique for reparameterizing any normal distribution from the standard one.

Related Functions

np-random-normalnp-random-randnp-random-seed