Full name
random.betavariate
Library
random
Syntax
random.betavariate(alpha, beta)
Description
The random.betavariate function returns a random number in the range (0, 1) extracted from a beta distribution.
Result
The random.betavariate function returns a real number.
Examples
We can generate a random number extracted from a beta distribution with parameters alpha = 5 and beta = 1 with the following code:
random.betavariate(5, 1)
0.9327378544132883
To confirm the distribution from which the random numbers are extracted, we can generate one hundred thousand random numbers from a beta distribution with alpha and beta parameters equal to 0.5 and show its histogram:
import matplotlib.pyplot as plt
plt.figure(figsize = (8, 4))
plt.hist([random.betavariate(0.5, 0.5) for i in range(100000)], bins = 100)
plt.show()
plt.hist([random.betavariate(0.5, 0.5) for i in range(100000)], bins = 100)
plt.show()