Python – Generate Random Float using Gaussian distribution
To generate a random floating point number using Gaussian distribution in Python, use gauss() function of Python random package.
In this tutorial, we shall learn how to generate a random floating point number based on Gaussian distribution with specified mean and standard deviation.
Syntax – random.guass()
Following is the syntax of gauss() function in random module.
f = random.gauss(mu, sigma)
where
Parameter | Description |
---|---|
mu | [Mandatory] Mean of Gaussian distribution. |
sigma | [Mandatory] Standard Deviation of Gaussian distribution. |
gauss() function returns a random floating point value based on the given mean and standard deviation for the Gaussian distribution.
Example 1
In this example, we shall use random.gauss() function to generate a random floating point number based on the Gaussian distribution with mean of 2 and standard deviation of 0.5.
Python Program
import random
mu = 2
sigma = 0.5
randomnumber = random.gauss(mu, sigma)
print(randomnumber)
Run Output
2.2072072627475663
Example 2
In this example, we shall use random.gauss() function to generate a random floating point number based on the Gaussian distribution with mean of 0 and standard deviation of 0.1.
Python Program
import random
mu = 0
sigma = 0.1
randomnumber = random.gauss(mu, sigma)
print(randomnumber)
Run Output
-0.040644379382734665
Summary
In this tutorial of Python Examples, we learned how to generate a random floating point number using Gaussian distribution, with the help of well detailed example programs.
Related Tutorials
- Python Program to Generate Random Password
- Python Numpy – Create Array with Random Values
- PyTorch Create Tensor with Random Values and Specific Shape
- Python – Generate a Random Number – Positive or Negative
- Python Program to Generate Random Float
- Python – Generate Random String of Specific Length
- Python Program to Flip a Coin
- Python Random Module Examples
- Python Program to Generate a Random Number of Specific Length