random
模塊用於在Python中生成隨機數。實際上不是隨機的,而是用於生成偽隨機數的。這意味著可以確定這些隨機生成的數字。
gammavariate()
gammavariate()
是內置的方法random
模塊。它用於返回具有伽馬分布的隨機浮點數。
用法: random.gammavariate(alpha, beta)
參數:
alpha:大於0
beta:大於0
返回:隨機伽馬分布浮點數
範例1:
# import the random module
import random
# determining the values of the parameter
alpha = 100
beta = 2
# using the gammavariate() method
print(random.gammavariate(alpha, beta))
輸出:
4.647425239687329
範例2:我們可以多次生成該數字並繪製圖形以觀察伽瑪分布。
# 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()
輸出:
範例3:我們可以創建一個直方圖來觀察伽瑪分布的密度。
# 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()
輸出:
相關用法
- Python Wand function()用法及代碼示例
- Python map()用法及代碼示例
- Python hex()用法及代碼示例
- Python now()用法及代碼示例
- Python cmp()用法及代碼示例
- Python oct()用法及代碼示例
- Python int()用法及代碼示例
- Python dir()用法及代碼示例
- Python id()用法及代碼示例
- Python tell()用法及代碼示例
- Python sum()用法及代碼示例
- Python ord()用法及代碼示例
- Python Wand fx()用法及代碼示例
- Python fsum()用法及代碼示例
- Python reversed()用法及代碼示例
- Python numpy.ma.where()用法及代碼示例
- Python math.sin()用法及代碼示例
注:本文由純淨天空篩選整理自Yash_R大神的英文原創作品 random.gammavariate() function in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。