Python:NumPy .normal()
Published Jul 19, 2025
Contribute to Docs
The .normal() function from NumPy’s random module generates random numbers from a normal (Gaussian) distribution, which is a continuous, bell-shaped distribution commonly used in statistics and data science.
Syntax
numpy.random.normal(loc=0.0, scale=1.0, size=None)
Parameters:
loc(float or array_like of floats, optional): The mean ($\mu$) of the distribution. Default is0.0.scale(float or array_like of floats, optional): The standard deviation ($\sigma$) of the distribution. Must be non-negative. Default is1.0.size(int or tuple of ints, optional): The output shape. Specifies the number or shape of random samples to generate. IfNone(default), a single sample is returned.
Return value:
out(ndarray or scalar): Random samples from the normal distribution.- If
sizeisNone, returns a single float. - If
sizeis specified, returns a NumPy array of the given shape with samples drawn from the normal distribution.
- If
Statistical Properties
- Mean ($μ$) =
loc - Variance ($σ^2$) = $\text{scale}^2$
The Probability Density Function (PDF) of the normal distribution is:
$$ p(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \ e^{-\frac{(x-\mu)^2}{2\sigma^2}} $$
- $\mu$ is the mean (
loc). - $\sigma$ is the standard deviation (
scale).
Example
This example generates two reproducible random samples from a normal distribution with loc = 0 (mean) and scale = 1 (standard deviation):
import numpy as npnp.random.seed(15)result = np.random.normal(loc=0, scale=1, size=2)print(result)
The output for this code will be:
[-0.44493578 0.22274619]
Here:
np.random.seed(15)ensures the same random numbers are generated every time the code runs.np.random.normal()draws two samples from the standard normal distribution.
Codebyte Example
This codebyte example generates a 2×3 array of normally distributed random numbers with loc = 5 (mean) and scale = 2 (standard deviation):
np.random.seed(42)sets the random seed for consistent results.size=(2, 3)creates a 2D array with 2 rows and 3 columns of normally distributed samples.
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
- 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