random
模塊用於在Python中生成隨機數。實際上不是隨機的,而是用於生成偽隨機數的。這意味著可以確定這些隨機生成的數字。
paretovariate()
paretovariate()
是內置的方法random
模塊。它用於返回具有Pareto分布的隨機浮點數。
用法: random.paretovariate(alpha)
參數:
alpha:shape參數
返回:隨機的帕累托分布浮點數
範例1:
# import the random module
import random
# determining the values of the parameter
alpha = 3
# using the paretovariate() method
print(random.paretovariate(alpha))
輸出:
1.0333528834896462
範例2:我們可以多次生成該數字並繪製圖形以觀察帕累托分布。
# import the required libraries
import random
import matplotlib.pyplot as plt
# store the random numbers in a
# list
nums = []
alpha = 3
for i in range(100):
temp = random.paretovariate(alpha)
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 = 3
for i in range(10000):
temp = random.paretovariate(alpha)
nums.append(temp)
# plotting a graph
plt.hist(nums, bins = 200)
plt.show()
輸出:
相關用法
- Python Wand function()用法及代碼示例
- Python cmp()用法及代碼示例
- Python dir()用法及代碼示例
- Python tell()用法及代碼示例
- Python map()用法及代碼示例
- Python int()用法及代碼示例
- Python hex()用法及代碼示例
- Python sum()用法及代碼示例
- Python ord()用法及代碼示例
- Python now()用法及代碼示例
- Python oct()用法及代碼示例
- Python id()用法及代碼示例
- Python round()用法及代碼示例
- Python randint()用法及代碼示例
- Python Sorted()用法及代碼示例
- Python cmath.tan()用法及代碼示例
注:本文由純淨天空篩選整理自Yash_R大神的英文原創作品 random.paretovariate() function in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。