random.lognormvariate

Full name
random.lognormvariate
Library
random
Syntax

random.lognormvariate(mu, sigma)

Description

The random.lognormvariate function returns a random number drawn from a log-normal distribution.

Parameters
  • mu: Mean of the distribution.
  • sigma: Standard deviation of the distribution.
Result

The random.lognormvariate function returns a real number.

Examples

We can generate a random number extracted from a log-normal distribution with mean 5 and standard deviation 3 with the following code:

random.lognormvariate(5, 3)
3.940389063724254

To confirm the distribution from which the random numbers are extracted, we can generate one hundred thousand random numbers from a log-normal distribution with mean 5 and standard deviation 3, obtain their natural logarithm and show their histogram:

import matplotlib.pyplot as plt
import math
plt.figure(figsize = (8, 4))
plt.hist([math.log(random.lognormvariate(5, 3)) for i in range(100000)], bins = 100)
plt.grid()
plt.show()
random.lognormvariate

We can see that it is indeed a normal distribution.

Submitted by admin on Tue, 03/16/2021 - 09:05