Python – Random Number using Gaussian Distribution

Contents

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 of random.guass()

Following is the syntax of gauss() function in random module.

``f = random.gauss(mu, sigma)``

where

gauss() function returns a random floating point value based on the given mean and standard deviation for the Gaussian distribution.

Examples

1. Generate float value using Gaussian Distribution

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 Code Copy

Output

``2.2072072627475663``

2. Generate float using Gaussian distribution with a standard deviation of 0.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 0 and standard deviation of 0.1.

Python Program

``````import random

mu = 0
sigma = 0.1
randomnumber = random.gauss(mu, sigma)
print(randomnumber)``````
Run Code Copy

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

Code copied to clipboard successfully 👍