Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.random.normal

Draws random samples from a normal (Gaussian) distribution. Set loc for the mean, scale for the standard deviation, and size for the output shape.

How it works

The function draws values from a normal distribution defined by its mean (loc) and standard deviation (scale). A scalar or array-like size controls the output shape; when omitted, scalar parameters produce one value, while broadcast-compatible parameter arrays determine the number of samples.

Syntax

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

Parameters

NameTypeDescription
locfloat or array_like of floats, optionalMean (center) of the distribution; accepts a float or array-like of floats.
scalefloat or array_like of floats, optionalStandard deviation (spread or width); accepts a float or array-like of floats and must be non-negative.
sizeint or tuple of ints, optionalOutput shape. An integer requests that many samples; a tuple specifies dimensions.

Returns

ndarray or scalar: drawn samples from the parameterized normal distribution.

Key points

  • It generates random values following a normal, also called Gaussian, distribution.
  • loc sets the distribution's mean, or center.
  • scale controls the distribution's spread through its standard deviation.
  • For a tuple shape, the function draws the product of its dimensions in samples.
  • Without an explicit size, array-like parameters produce as many samples as their broadcasted shape contains.

Common mistakes

  • scale represents standard deviation and cannot be negative; validate this input before calling the function.
  • For new code, prefer calling normal on a default_rng() Generator rather than using this legacy convenience function.

Example: Generate normally distributed samples

import numpy as np

rng = np.random.default_rng(42)
samples = rng.normal(loc=10.0, scale=2.0, size=(2, 3))
print(samples.shape)  # (2, 3)

Creates six reproducible samples centered at 10 with standard deviation 2, arranged as a 2-by-3 array.

When to use it

Simulation

Create synthetic measurements or perturbations around a target mean with controlled variability.

Synthetic data generation

Produce random feature or parameter values for experiments and prototypes.

Noise modeling

Model Gaussian noise by sampling with a chosen standard deviation.

Interview questions

QWhat do loc and scale mean?

loc is the distribution mean, and scale is its standard deviation. scale must be non-negative.

Follow-up: What happens when loc and scale are arrays? Their broadcast-compatible values determine the sample parameters; with size=None, the output contains the broadcast size.

QHow does the size argument control output?

size specifies the output shape. An integer requests that many samples, while a tuple creates an array with the requested dimensions.

Follow-up: How does size=None differ? Scalar parameters return one scalar; otherwise the number of samples equals the size of the broadcasted parameters.

QWhat does np.random.normal return, and what API is preferred for new code?

The function returns scalar or ndarray samples from the parameterized normal distribution. With scalar parameters and no explicit size, the result is a single value.

Follow-up: Which API should new code use? Create a Generator with np.random.default_rng() and call its normal method.

  • numpy.random.Generator.normal
  • numpy.random.default_rng
  • numpy.random.normal

Source: numpy.random.normal. Checked 2026-10-15.