The numpy.random.randn() function generates random floating-point samples drawn from the standard normal (Gaussian) distribution with a mean of μ = 0 and standard deviation of σ = 1 (often written as N(0, 1)).
Parameter Note: Like its companion numpy.random.rand(), randn() accepts array dimensions as separate positional arguments (e.g. np.random.randn(3, 4)) rather than a shape tuple. For uniform numbers over [0, 1), see rand() and random_sample(). For discrete integers, see randint(). For modern reproducible code, explore the recommended default_rng() Generator interface.
The formal syntax for numpy.random.randn() is:
# Formal signature
numpy.random.randn(d0, d1, ..., dn)
| Argument | Type | Default | Description & Operational Rule |
|---|---|---|---|
d0, d1, ..., dn |
int (optional) |
None | The dimensions of the returned array. If no arguments are passed, a single Python float is returned. |
| Return | ndarray or float |
N/A | A (d0, d1, ..., dn) shaped array of floating-point values from the standard normal distribution, or a single float if no dimensions are supplied. |
Calling np.random.randn() with no arguments returns a scalar float drawn from standard normal distribution N(0, 1):
import numpy as np
# Set seed for reproducible tutorial verification
np.random.seed(42)
my_data = np.random.randn()
print("Scalar value:", my_data)
print("Data type: ", type(my_data))
Terminal Output:
Scalar value: 0.4967141530112327
Data type: <class 'float'>
Supplying a single integer generates a 1D ndarray containing standard normal floats:
import numpy as np
np.random.seed(10)
my_vector = np.random.randn(4)
print("1D Vector:\n", my_vector)
print("Shape: ", my_vector.shape)
print("Dtype: ", my_vector.dtype)
Terminal Output:
1D Vector:
[ 1.3315865 0.71527897 -1.54540029 -0.00838385]
Shape: (4,)
Dtype: float64
Pass two integer arguments (rows, columns) to construct a 2D matrix:
import numpy as np
np.random.seed(100)
my_matrix = np.random.randn(4, 3)
print("2D Matrix (4 rows, 3 columns):\n", my_matrix)
print("Matrix Dimensions (ndim):", my_matrix.ndim)
Terminal Output:
2D Matrix (4 rows, 3 columns):
[[-1.74976547 0.3426804 1.1530358 ]
[-0.25243603 0.98132079 0.51421884]
[ 0.22117967 -1.07004333 -0.18949583]
[ 0.25500144 -0.45802699 0.43516349]]
Matrix Dimensions (ndim): 2
Multiple dimension arguments produce higher-dimensional tensors useful in machine learning and Monte Carlo simulations:
import numpy as np
np.random.seed(7)
tensor_4d = np.random.randn(1, 2, 3, 4)
print("4D Tensor shape:", tensor_4d.shape)
print("Total elements: ", tensor_4d.size)
Terminal Output:
4D Tensor shape: (1, 2, 3, 4)
Total elements: 24
Because randn() draws from the standard normal distribution (μ = 0, σ = 1), samples can be transformed to any normal distribution with mean μ and standard deviation σ using the linear formula:
Sample = μ + σ * np.random.randn(...)
import numpy as np
# Target: Mean = 50.0, Standard Deviation = 5.0
mu = 50.0
sigma = 5.0
np.random.seed(42)
samples = mu + sigma * np.random.randn(10000)
print("Empirical Mean: ", np.mean(samples))
print("Empirical StdDev:", np.std(samples))
Terminal Output:
Empirical Mean: 49.991873
Empirical StdDev: 4.966326
| Function | Distribution Type | Output Range | Input Arguments |
|---|---|---|---|
np.random.randn() |
Standard Normal (Gaussian) | (−∞, +∞), centered at 0 | Separate dimensions: d0, d1, ... |
np.random.rand() |
Continuous Uniform | [0.0, 1.0) | Separate dimensions: d0, d1, ... |
np.random.randint() |
Discrete Uniform Integers | [low, high) | Limits + size=tuple |
np.random.random_sample() |
Continuous Uniform | [0.0, 1.0) | Shape tuple: size=(r, c) |
NumPy documentation recommends the modern Generator interface (introduced in NumPy 1.17) for new code. It avoids global mutable seed state and accepts a standard shape tuple:
import numpy as np
rng = np.random.default_rng(42)
# Direct specification of loc (mean), scale (std), and shape tuple
modern_samples = rng.normal(loc=0.0, scale=1.0, size=(3, 3))
print("Generated via default_rng().normal():\n", modern_samples)
np.random.randn((3, 4)) raises a TypeError: 'tuple' object cannot be interpreted as an integer. Pass np.random.randn(3, 4).rand() which is strictly ≥ 0, approximately 50% of randn() values are negative.Practice generating univariate normals, simulating multivariate Gaussian noise, scaling distributions, and running self-verifying unit assertions in our companion notebook:
Launch the notebook directly in Google Colab or view the source notebook in GitHub:
View on GitHubrandn() and rand() are legacy methods inspired by MATLAB syntax that accept dimensions as separate positional arguments (*args). Passing a tuple passes a single argument of type tuple, which cannot be converted to an integer dimension. Use np.random.randn(3, 4) or unpack with *shape.
numpy.random.seed(value) before generating numbers. For modern applications, create an independent generator instance with rng = numpy.random.default_rng(value) and use rng.normal(size=...).
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.