A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/numpy/normal-distribution-in-numpy/ below:

Normal Distribution in NumPy - GeeksforGeeks

Normal Distribution in NumPy

Last Updated : 23 Apr, 2025

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

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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4