不应在平均值和标准差之间使用random.gauss返回值吗?

时间:2018-11-30 05:42:18

标签: python python-3.x random

我尝试在Python中使用random.gauss函数来获取范围之间的随机值。高斯函数将平均值,标准差作为参数。返回值不应该在[mean +- standard deviation]之间吗?

下面是代码段:

for y in [random.gauss(4,1) for _ in range(50)]:
    if y > 5 or y < 3: # shouldn't y be between  (3, 5) ?
        print(y)

代码输出:

6.011096878888296
2.9192195126660403
5.020299287583643
2.9322959456674083
1.6704559841869528

3 个答案:

答案 0 :(得分:2)

“返回值是否应该在[平均值+-标准差]之间?”

不。 For the Gaussian distribution slightly less than 32% of the outcomes will be more than one standard deviation away from the mean in either direction。没有包含100%结果的固定范围。

答案 1 :(得分:0)

高斯分布仅关注值偏离均值一定量的概率。因此,任何值都是可能的,只是在某些值范围内概率很低

答案 2 :(得分:-1)

样本数量50不足以使样本平均值和样本SD成为总体平均值/ SD的良好估计。 Law of large numbers to the rescue

from random import gauss

xs = []

for i in range(100000):
    xs.append(gauss(0,1))

print("mean = ", sum(xs)/len(xs)) # should print a number close to 0
in_sd = len(list(x for x in xs if x > -1 and x < 1))
print("in SD = ",  in_sd/len(xs)) # should print a number close to 0.68

https://repl.it/@millimoose/SiennaFlatBackground


要获取3到5之间的随机数,请改用random.uniform (3, 5)