Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the random.normalvariate() method in Python?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 random
mu = 5
sigma = 4.3
val = random.normalvariate(mu, sigma=sigma)
print("random.normalvariate(%s, %s) = %s" % (mu, sigma, val))

Explanation

  • Line 1: We import the random module.
  • 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 pass mu and sigma as parameters in the variable val.
  • Line 7: We print the variable val to console.

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring