Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python random.lognormvariate() Method



The random.lognormvariate() method in Python generates random numbers that follows the Log normal (lognormal) Distribution. This distribution is a family of continuous probability distributions of a random variable whose logarithm is normally distributed.

It depends on two parameters, mu and sigma, where mu is the mean and sigma is the standard deviation of the underlying normal distribution.

The log-normal distribution is often used in natural sciences, engineering, medicine, economics, and other fields.

This function is not accessible directly, so we need to import the random module and then we need to call this function using random static object.

Syntax

Following is the syntax of lognormvariate() method −

random.lognormvariate(mu, sigma)

Parameters

The Python random.lognormvariate() method takes two parameters −

  • mu: This is the mean of the underlying normal distribution (the natural logarithm of the log-normal distribution). It can take any real value.

  • sigma: This is the standard deviation of the underlying normal distribution. It must be greater than zero.

Return Value

This random.lognormvariate() method returns a random number that follows the Log normal distribution.

Example 1

Let's see a basic example of using the random.lognormvariate() method for generating a random number from a normal distribution with a mean of 0 and a standard deviation of 1.

import random

# mean
mu = 0  
# standard deviation
sigma = 1  

# Generate a log normal-distributed random number
random_number = random.lognormvariate(mu, sigma)

# Print the output
print("Generated random number from log normal distribution:",random_number)

Following is the output −

Generated random number from log normal distribution: 9.472544796309364

Note: The Output generated will vary each time you run the program due to its random nature.

Example 2

This example generates a list of 10 random numbers that follows the log normal distribution using the random.lognormvariate() method.

import random

# mean
mu = 0

# standard deviation
sigma = 0.5  

result = []

# Generate a list of random numbers from the log normal distribution
for i in range(10):
    result.append(random.lognormvariate(mu, sigma))

print("List of random numbers from log normal distribution:", result)

While executing the above code you will get the similar output like below −

List of random numbers from log normal distribution: [0.500329149795808, 1.7367179979113172, 0.5143664713594474, 0.5493391936855808, 1.3565058546966193, 1.4841135680348012, 0.5950837276748621, 0.8880005878135713, 1.0527856543498058, 0.7471389015523113]

Example 3

Here is another example that uses the random.lognormvariate() method, and demonstrates how changing the mean and standard deviation affects the shape of the normal distribution.

import random
import matplotlib.pyplot as plt

# Define a function to generate and plot data for a given mu and sigma
def plot_log_norm(mu, sigma, label, color):

    # Generate log normal-distributed data
    data = [random.lognormvariate(mu, sigma) for _ in range(10000)]

    # Plot histogram of the generated data
    plt.hist(data, bins=100, density=True, alpha=0.8, color=color, label=f'(mu={mu}, sigma={sigma})')

fig = plt.figure(figsize=(7, 4))

# Plotting for each set of parameters
plot_log_norm(0, 1, '0, 1', 'blue')
plot_log_norm(0, 0.5, '0, 0.5', 'green')
plot_log_norm(0, 0.25, '0, 0.25', 'yellow')

# Adding labels and title
plt.title('Log Normal Distributions')
plt.legend()

# Show plot
plt.show()

The output of the above code is as follows −

Random Lognormvariate Method
Advertisements