Simulation
Create synthetic measurements or perturbations around a target mean with controlled variability.
NumPy · Function
Draws random samples from a normal (Gaussian) distribution. Set loc for the mean, scale for the standard deviation, and size for the output shape.
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.
numpy.random.normal(loc=0.0, scale=1.0, size=None)| Name | Type | Description |
|---|---|---|
loc | float or array_like of floats, optional | Mean (center) of the distribution; accepts a float or array-like of floats. |
scale | float or array_like of floats, optional | Standard deviation (spread or width); accepts a float or array-like of floats and must be non-negative. |
size | int or tuple of ints, optional | Output shape. An integer requests that many samples; a tuple specifies dimensions. |
ndarray or scalar: drawn samples from the parameterized normal distribution.
loc sets the distribution's mean, or center.scale controls the distribution's spread through its standard deviation.scale represents standard deviation and cannot be negative; validate this input before calling the function.normal on a default_rng() Generator rather than using this legacy convenience function.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.
Create synthetic measurements or perturbations around a target mean with controlled variability.
Produce random feature or parameter values for experiments and prototypes.
Model Gaussian noise by sampling with a chosen standard deviation.
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.
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.
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.normalnumpy.random.default_rngnumpy.random.normal