0
0
NumPydata~5 mins

Normal distribution with normal() in NumPy

Choose your learning style9 modes available
Introduction

We use the normal() function to create numbers that follow a bell-shaped pattern. This helps us model real-world things like heights or test scores.

When you want to simulate real-world data like people's heights or weights.
To test how algorithms work with data that looks like natural measurements.
When you need random numbers that cluster around an average value.
For creating sample data to practice statistics or machine learning.
To understand probabilities and variations in natural phenomena.
Syntax
NumPy
numpy.random.normal(loc=0.0, scale=1.0, size=None)

loc is the average (mean) value where the data centers.

scale is how spread out the data is (standard deviation).

Examples
Generates one number from a normal distribution with mean 0 and standard deviation 1.
NumPy
numpy.random.normal()
Generates 3 numbers centered around 5 with spread 2.
NumPy
numpy.random.normal(loc=5, scale=2, size=3)
Generates 5 numbers from the standard normal distribution (mean 0, std 1).
NumPy
numpy.random.normal(size=5)
Sample Program

This code creates 10 random numbers that are mostly around 100 but can vary by about 15. It then prints the numbers, their average, and how spread out they are.

NumPy
import numpy as np

# Generate 10 random numbers from a normal distribution
# with mean 100 and standard deviation 15
data = np.random.normal(loc=100, scale=15, size=10)

print('Generated data:', data)
print('Mean of data:', np.mean(data))
print('Standard deviation of data:', np.std(data))
OutputSuccess
Important Notes

Each time you run the code, you get different numbers because they are random.

You can set a random seed with np.random.seed(number) to get the same results every time.

Summary

The normal() function creates random numbers that follow a bell curve.

You control the center with loc and the spread with scale.

This helps simulate and understand real-world data that varies naturally.