Python random.normalvariate()
Python random.normalvariate()
The random module is used to generate random numbers in Python. These numbers are not actually random, but rather pseudo-random. This means that the numbers generated are deterministic.
random.normalvariate()
normalvariate() is a built-in method of the random module. It returns a random floating-point number with a normal distribution.
Syntax: Random.normalvariate(mu, sigma).
Parameters:
mu: mean
igma: standard deviation
Returns: A random normally distributed floating-point number
Example 1:
# import the random module
import random
# determining the values of the parameters
mu = 100
sigma = 50
# using the normalvariate() method
print(random.normalvariate(mu, sigma))
Output:
110.86699628241412
Example 2: We can generate numbers multiple times and plot a graph to observe the normal distribution.
# import the required libraries
import random
import matplotlib.pyplot as plt
# store the random numbers in a
# list
nums = []
mu = 100
sigma=50
for i in range(100):
Temp = random.normalvariate(mu, sigma)
nums.append(temp)
# plotting a graph
plt.plot(nums)
plt.show()
Output:
Example 3: We can create a histogram to visualize the density of a normal distribution.
# import the required libraries
import random
import matplotlib.pyplot as plt
# store the random numbers in a list
nums = []
mu = 100
sigma=50
for i in range(10000):
Temp = random.normalvariate(mu, sigma)
nums.append(temp)
# plotting a graph
plt.hist(nums, bins = 200)
plt.show()
Output: