Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python random.expovariate() Method



Therandom.expovariate() method in Python generates random numbers that follows theExponential distribution. The exponential distribution is a continuous probability distribution commonly used to model the time between events in a Poisson process. It is characterized by a parameterlambda, which is the rate parameter.

The parameter lambda is 1.0 divided by the desired mean of the distribution. If lambda is positive, the function returns values from 0 to positive infinity, representing times between events. If lambda were negative, it would return values from negative infinity to 0.

Syntax

Following is the syntax of theexpovariate() method −

random.expovariate(lambda)

Parameters

This method accepts a single parameter −

  • lambda: This is the rate parameter of the exponential distribution.

Return Value

This method returns random numbers that follows the exponential distribution with the specified rate.

Example 1

Let's see a basic example of using therandom.expovariate() method for generating a single random number.

import random# Lambda for the Exponential distributionlambda_ = 2# Generate a random number from the Exponential distributionrandom_value = random.expovariate(lambda_)print("Random value from Exponential distribution:", random_value)

Following is the output −

Random value from Exponential distribution: 0.895003194051671

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

Example 2

This example generates 10 interval times with an average rate of 15 arrivals per second using therandom.expovariate() method.

import random# Lambda for the Exponential distributionrate = 15  # 15 arrivals per second# Generate a random numbers from the Exponential distributionfor i in range(10):    interarrival_time = random.expovariate(rate)    print(interarrival_time)

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

0.055359397226710010.03652947738387890.07081900087488210.119204228531226640.0149663946413572580.059367961311613080.091688158514955130.184265758507790560.035335917688278030.08367815594819812

Example 3

Here is another example that uses therandom.expovariate() method to generate and display a histogram showing the frequency distribution of the integer parts of samples from an exponential distribution with a rate parameter of 100.

import randomimport numpy as npimport matplotlib.pyplot as plt# Generate 10000 samples from an exponential distribution with rate parameter of 100rate = 1 / 100  num_samples = 10000 # Generate exponential data and convert to integersd = [int(random.expovariate(rate)) for _ in range(num_samples)]# Create a histogram of the data with bins from 0 to the maximum value in dh, b = np.histogram(d, bins=np.arange(0, max(d)+1))# Plot the histogramplt.bar(b[:-1], h, width=1, edgecolor='none')plt.title('Histogram of Integer Parts of Exponentially Distributed Data')plt.show()

The output of the above code is as follows −

Random Expovariate Method
python_modules.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp