Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Generate five Random Numbers from the Normal Distribution using NumPy
In the study of statistics and data analysis, normal distribution or Gaussian Distribution is a widely used probability distribution. It is a bell-shaped curve that characterizes probability and is often used to model real-world phenomena.
We use the random module available in Python's NumPy library to generate random numbers from the normal distribution. It allows users to generate random numbers with a specified mean and standard deviation.
Syntax
numpy.random.normal(loc=0.0, scale=1.0, size=None)
Parameters
loc (float or array_like): The mean or center of the distribution. Default value is 0.0 and represents the peak of the bell curve.
scale (float or array_like): The standard deviation of the distribution. Default value is 1.0 and controls the width of the bell curve.
size (int or tuple of ints): The shape of the output that determines the number of random numbers to be generated. When a tuple is provided, it generates a multi-dimensional array. Default value is None.
Example 1: Default Mean and Standard Deviation
Generate five random numbers using default parameters (mean=0, std=1) ?
import numpy as np
# Generate five random numbers from the normal distribution
random_numbers = np.random.normal(size=5)
# Print the random numbers
print("Random Generated Numbers:", random_numbers)
Random Generated Numbers: [-0.66362634 0.60882755 0.62147686 -0.0246644 0.17017737]
Example 2: Custom Mean and Standard Deviation
Generate ten random numbers with custom mean=10 and standard deviation=2 ?
import numpy as np
# Setting custom mean and standard deviation
mean = 10 # Mean of the distribution
std_dev = 2 # Standard deviation of the distribution
# Generate ten random numbers
random_numbers = np.random.normal(loc=mean, scale=std_dev, size=10)
# Print the generated random numbers
print("Generated Random Numbers:", random_numbers)
Generated Random Numbers: [10.81862559 7.28414504 8.61239397 8.98294608 7.50709111 7.90727366 9.21915208 10.43019622 12.493977 11.57399687]
Example 3: Two-Dimensional Array
Generate a 2D array (4×5) of random numbers from normal distribution ?
import numpy as np
# Set the mean and standard deviation
mean = 0
std_dev = 1
# Generate a 2D array of random numbers
random_numbers = np.random.normal(loc=mean, scale=std_dev, size=(4, 5))
# Print the generated random numbers
print("Two-dimensional array of random numbers:")
print(random_numbers)
Two-dimensional array of random numbers: [[-1.18743672 -1.32939008 0.37248625 0.31413006 -0.83207142] [-1.26353284 0.4993038 -1.02139944 -0.66408169 -0.40570098] [-1.36817096 -0.05267991 -0.33518531 -0.0784531 -0.34882078] [ 1.3996869 0.53987652 -2.59857656 -1.2062663 -1.83573899]]
Common Use Cases
| Application | Description | Typical Parameters |
|---|---|---|
| Statistical Simulations | Model random variables | Custom mean/std |
| Monte Carlo Methods | Random sampling for estimation | Large sample sizes |
| Financial Modeling | Asset returns simulation | Market-specific parameters |
Conclusion
NumPy's random.normal() function provides flexible random number generation from normal distributions. Use default parameters for standard normal distribution, or customize mean and standard deviation for specific applications like financial modeling and statistical simulations.
