Python random.gammavariate()

Python random.gammavariate()

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.gammavariate()

gammavariate() is a built-in method of the random module. It returns a random floating-point number with a gamma distribution.

Syntax: Random.gammavariate(alpha, beta).

Parameters:

alpha: greater than 0

beta: greater than 0

Returns: A random gamma-distributed floating-point number

Example 1:

# import the random module
import random
  
# determining the parameter values
alpha = 100
beta = 2
  
# using the gammavariate() method
print(random.gammavariate(alpha, beta))

Output:

4.647425239687329

Example 2: We can generate numbers multiple times and plot a graph to observe the gamma distribution.

# import the required libraries
import random
import matplotlib.pyplot as plt
    
# store the random numbers in a
# list
nums = []
alpha=9
beta=0.5
    
for i in range(100):
Temp = random.gammavariate(alpha, beta)
nums.append(temp)
        
# plotting a graph 
plt.plot(nums) 
plt.show()

Output:

Python random.gammavariate

Example 3: We can create a histogram to visualize the density of the gamma distribution.

# import the required libraries
import random
import matplotlib.pyplot as plt
    
# store the random numbers in a list
nums = []
alpha=9
beta=0.5
    
for i in range(10000):
Temp = random.gammavariate(alpha, beta)
nums.append(temp)
        
# plotting a graph
plt.hist(nums, bins = 200)
plt.show()

Output:

Python random.gammavariate

Leave a Reply

Your email address will not be published. Required fields are marked *