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 – 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.
Example 1
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)
Run Output
2.3033833601115106
Example 2
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)
Run 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.
Related Tutorials
- Python Numpy – Create Array with Random Values
- Python – Generate Random String of Specific Length
- Python Program to Generate Random Password
- Python – Generate a Random Number – Positive or Negative
- Python Program to Generate a Random Number of Specific Length
- Python Program to Flip a Coin
- Python Random Module Examples
- Python Program to Generate Random Float
- PyTorch Create Tensor with Random Values and Specific Shape