.randn()
Published May 14, 2025
Contribute to Docs
The .randn()
function generates an array of random numbers sampled from the standard normal distribution (a Gaussian distribution where mean = 0 and standard deviation = 1). It is commonly used in statistics, machine learning, and data analysis for creating synthetic data and testing algorithms.
Syntax
numpy.random.randn(d0, d1, ..., dn)
Parameters:
d0, d1, ..., dn
: Dimensions of the output array. If no arguments are provided, the.randn()
function returns a single random float sampled from the standard normal distribution.
Return value:
- The
.randn()
function returns anndarray
of shape (d0, d1, ..., dn
) filled with random samples from the standard normal distribution.
Example
In this example, the .randn()
generates a 2x3 NumPy array filled with random numbers from the standard normal distribution:
import numpy as np# Generate a 2x3 array of random samplessamples = np.random.randn(2, 3)# Print the resultprint(samples)
A possible output of this code can be:
[[-1.87894354 -0.05884307 1.0121173 ][ 0.77652245 0.20369627 -0.97778735]]
Note: The output may change each time the code is run because the values are generated randomly from a standard normal distribution.
Codebyte Example
This codebyte generates a 1-dimensional array with 5 elements and a 3-dimensional array with shape (2, 2, 2) using numpy.random.randn()
:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours