python random.normalvariate(),正态分布随机数

random.normalvariate()方法

python的random模块中的normalvariate()方法,可以用于获取一个正态分布的随机数。语法如下:


语法

random.normalvariate(mu, sigma)

python中源码对于该方法的介绍如下:

 method of random.Random instance
    Normal distribution.
    
    mu is the mean, and sigma is the standard deviation.

参数

参数描述
mu必须参数,源码中的介绍 mu is the mean,即平均数
sigma必须参数,源码中的介绍 sigma is the standard deviation,即标准差

提示:random.normalvariate()方法必须接收两个参数,否则python抛出TypeError。


random.normalvariate实例代码

>>> import random
>>> random.normalvariate(3,0.1)
3.063420843165815
>>> random.normalvariate(5) #当只传递一个参数时
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: normalvariate() missing 1 required positional argument: 'sigma'
>>> random.normalvariate() #当没有传递参数时
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: normalvariate() missing 2 required positional arguments: 'mu' and 'sigma'

全栈后端 / Python库 :









Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.