
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate Five Random Numbers from 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 the 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 also allows users to generate random numbers from the normal distribution 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): It is the mean or centre of the distribution. The default value is 0.0 and represents the peak of the bell curve thus produced.
scale (float or array_like): it is the standard deviation of the distribution. The default value is 1.0 and this controls the width of the bell curve.
size (int or tuple of ints): It returns the shape of the output and determines the number of random numbers to be generated.
When a tuple of integers is provided, the funtion generates a multi-dimensional array of random numbers with the specified shape. Its default value is marked as None.
Example 1
The following example shows how to Generate random numbers with default mean and standard deviation.
Algorithm
Import the numpy library.
Use the random.normal function with no explicit mean and standard deviation parameters.
Specify the size parameter as 5 to generate five random numbers from the normal distribution.
Store the generated random numbers in the variable random_numbers.
Print the random_numbers variable.
import numpy as nmp # To generate five random numbers from the normal distribution random_numbers = nmp.random.normal(size=5) # Print the random numbers print("The Random Generated Numbers Are:", random_numbers)
Output
The Random Generated Numbers Are: [-0.66362634 0.60882755 0.62147686 -0.0246644 0.17017737]
Example 2
The following code illustrates Generating ten random numbers from the normal distribution with custom mean and standard deviation using NumPy.
import numpy as np # Setting the custom mean and standard deviation mean = 10 # Mean of the distribution std_dev = 2 # Standard deviation of the distribution # Generate the random numbers random_numbers = np.random.normal(loc=mean, scale=std_dev, size=10) # Print the generated random numbers print("Here Are The 10 Generated Random Numbers:", random_numbers)
Output
Here Are The 10 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
In this example, we will generate a Two-Dimensional 4 by 5 array of random numbers from the Normal Distribution using NumPy with user-defined mean and standard deviation.
Algorithm
Import the numpy library as np.
Set the desired mean and std_dev values for the normal distribution.
Use the numpy.random.normal function and provide the mean and strd_dev values as arguments.
Specify the size parameter as a tuple (4, 5) to generate a 2D array of random numbers with 4 rows and 5 columns from the normal distribution.
Store the generated random numbers in the variable random_numbers.
Print the random_numbers variable.
import numpy as nmpy # Set the mean and standard deviation mean = 0 strd_dev = 1 # Generate a 2D array of random numbers from the normal distribution random_numbers = nmpy.random.normal(loc=mean, scale=strd_dev, size=(4, 5)) # Print the generated random numbers print("The two-dimensional array of random numbers:") print(random_numbers)
Output
The 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]]
Conclusion
The ability to customize the mean and standard deviation of the normal distribution allows for a wide range of use cases like Statistical Simulations, Data Analysis, Monte Carlo simulations involving random sampling to estimate and analyze complex systems and Financial Modeling.
In finance and investment analysis, the normal distribution is often used to model asset returns. It also enables the simulation of different investment scenarios and risk assessments.