What is the random.normalvariate() method in Python?
Overview
The normalvariate method in Python is used to get a floating-point value that is chosen from a Gaussian/normal distribution with the given mean and standard deviation.
What is Gaussian or normal distribution?
The Gaussian distribution, also known as normal distribution, is a symmetric probability distribution centered on the mean, which indicates that the data near the mean occurs more frequently than data that is far from it. Normal distribution will appear as a bell curve on a graph.
The following image shows us what a bell curve looks like when plotted on a graph:
Syntax
random.normalvariate(mu, sigma)
Parameter values
mu: This is the mean value.sigma: This is the standard deviation value.
Return value
This method returns a floating-point value.
Code
import randommu = 5sigma = 4.3val = random.normalvariate(mu, sigma=sigma)print("random.normalvariate(%s, %s) = %s" % (mu, sigma, val))
Explanation
- Line 1: We import the
randommodule. - Line 3: We define the mean value
mu. - Line 4: We define the standard deviation value
sigma. - Line 5: We store the value that is returned by the
normalvariate()method, as we passmuandsigmaas parameters in the variableval. - Line 7: We print the variable
valto console.