python random.gauss(),高斯分布随机数
random.gauss()方法
python的random模块中的gauss()方法,可以用于返回一个高斯分布的随机数。顺口一提,在图像处理软件当中,就有款叫做高斯模糊的滤镜,当然了,在python的机器学习当中,也会应用到高斯分布的算法。
语法
random.gauss(mu, sigma)
以下部分是来自python源码中对gauss()随机数方法的介绍:
Gaussian distribution.
mu is the mean, and sigma is the standard deviation. This is
slightly faster than the normalvariate() function.
Not thread-safe without a lock around calls.
参数
正如上面python源码中的介绍:
参数 | 描述 |
---|---|
mu | 必须参数,第一个参数mu为mean平均数 |
sigma | 必须参数,第二个参数sigma为standard deviation标准差 |
提示:mu和sigma为必须参数,如果不传递参数,python会抛出TypeError。
random.gauss()实例代码
>>> import random
>>> random.gauss(2,0.5)
1.8916598123240076
>>> random.gauss() #没有参数,python抛出TypeError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: gauss() missing 2 required positional arguments: 'mu' and 'sigma'