Python - Random Number using Normal Distribution
Python - Generate Random Float using Normal distribution
To generate a random floating point number using Normal distribution in Python, use normalvariate() function of Python random package.
In this tutorial, we shall learn how to generate a random floating point number based on Normal distribution with specified mean and standard deviation.
Syntax of random.normalvariate()
Following is the syntax of normalvariate() function in random module.
f = random.normalvariate(mu, sigma)
where
Parameter | Description |
---|---|
mu | Mean of Normal distribution. |
sigma | Standard Deviation of Normal distribution. |
normalvariate() function returns a random floating point value based on the given mean and standard deviation for the Normal distribution.
Examples
1. Generate a float value using normal distribution
In this example, we shall use random.normalvariate() function to generate a random floating point number based on the Normal distribution with mean of 2 and standard deviation of 0.5.
Python Program
import random
mu = 2
sigma = 0.5
randomnumber = random.normalvariate(mu, sigma)
print(randomnumber)
Output
2.3033833601115106
2. Generate a float value using normal distribution with a standard deviation of 1
In this example, we shall use random.normalvariate() function to generate a random floating point number based on a Normal distribution with mean of 0 and standard deviation of 0.1.
Python Program
import random
mu = 0
sigma = 0.1
randomnumber = random.normalvariate(mu, sigma)
print(randomnumber)
Output
0.10676590503788591
Summary
In this tutorial of Python Examples, we learned how to generate a random floating point number using Normal distribution, with the help of well detailed example programs.