random.betavariate() method in Python
betavariate() is an inbuilt method of the random module. It is used to return a random floating point number with beta distribution. The returned value is between 0 and 1.
Syntax : random.betavariate(alpha, beta)
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Parameters :
alpha : greater than 0
beta : greater than 0Returns : a random beta distribution floating number between 0 and 1
Example 1:
# import the random moduleimport random # determining the values of the parametersalpha = 5beta = 10 # using the betavariate() methodprint(random.betavariate(alpha, beta)) |
Output :
0.5148685287422776
Example 2: We can generate the number multiple times and plot a graph to observe the beta distribution.
# import the required librariesimport randomimport matplotlib.pyplot as plt # store the random numbers in a # listnums = []low = 10high = 100mode = 20 for i in range(100): temp = random.betavariate(5, 10) nums.append(temp) # plotting a graphplt.plot(nums)plt.show() |
Output :


