Open In App

Normal Distribution in NumPy

Last Updated : 23 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Normal Distribution also known as the Gaussian Distribution is one of the most important distributions in statistics and data science. It is widely used to model real-world phenomena such as IQ scores, heart rates, test results and many other naturally occurring events.

numpy.random.normal() Method

In Python's NumPy library we can generate random numbers following a Normal Distribution using the numpy.random.normal() method. It has three key parameters:

  1. loc : Specifies the center (mean) of the distribution, where the peak of the bell curve exists.
  2. scale : Determines the spread (standard deviation) of the distribution controlling how flat or narrow the graph is.
  3. size : Defines the shape of the returned array.

Syntax :

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

Example 1: Generate a Single Random Number

To generate a single random number from a default Normal Distribution (loc=0, scale=1):

Python
import numpy as np

random_number = np.random.normal()
print(random_number)

Output:

0.013289272641035141

Example 2: Generate an Array of Random Numbers

To generate multiple random numbers

Python
random_numbers = np.random.normal(size=5)
print(random_numbers)

Output:

[ 1.44819595 0.90517495 -0.75923069 0.50357022 -0.34776612]

Visualizing the Normal Distribution

Visualizing the generated numbers helps in understanding their behavior. Below is an example of plotting a histogram of random numbers generated using numpy.random.normal.

Python
import numpy as np
import matplotlib.pyplot as plt

data = np.random.normal(loc=0, scale=1, size=1000)

plt.hist(data, bins=30, edgecolor='black', density=True)
pdf = norm.pdf(x, loc=loc, scale=scale)  
plt.plot(x, pdf, color='red', label='Theoretical PDF')
plt.title("Normal Distribution")
plt.xlabel("Value")
plt.ylabel("Density")
plt.grid(True)
plt.show()

Output:

Normal-Distribution
Normal Distribution

The histogram represents the frequency of the generated numbers and the curve shows the theoretical pattern for comparison. The curve of a Normal Distribution is also known as the Bell Curve because of the bell-shaped curve.


Next Article
Article Tags :
Practice Tags :

Similar Reads