Python:NumPy .randn()

object2410038751's avatar
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.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

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 an ndarray 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 samples
samples = np.random.randn(2, 3)
# Print the result
print(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():

Code
Output

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours