📜  Python中的 random.gauss()函数

📅  最后修改于: 2022-05-13 01:54:30.855000             🧑  作者: Mango

Python中的 random.gauss()函数

random模块用于在Python中生成随机数。实际上不是随机的,而是用于生成伪随机数。这意味着可以确定这些随机生成的数字。

随机高斯()

gauss()random模块的内置方法。它用于返回具有高斯分布的随机浮点数。

示例 1:

# import the random module
import random
  
# determining the values of the parameters
mu = 100
sigma = 50
  
# using the gauss() method
print(random.gauss(mu, sigma))

输出 :

127.80261974806497

示例 2:我们可以多次生成数字并绘制图表以观察高斯分布。

# import the required libraries 
import random 
import matplotlib.pyplot as plt 
    
# store the random numbers in a  
# list 
nums = [] 
mu = 100
sigma = 50
    
for i in range(100): 
    temp = random.gauss(mu, sigma)
    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 = [] 
mu = 100
sigma = 50
    
for i in range(10000): 
    temp = random.gauss(mu, sigma) 
    nums.append(temp) 
        
# plotting a graph 
plt.hist(nums, bins = 200) 
plt.show()

输出 :