【发布时间】:2014-07-16 10:09:53
【问题描述】:
尝试的问题:两个骰子中的一个比第三个骰子的值更高的概率。
问题:由于某种原因,当我使用 python 中的 random 模块(特别是示例方法)时,我最终得到了与使用 numpy 时不同(且不正确)的结果.我已将结果包含在底部。重复执行代码会产生类似的结果。任何想法,为什么random.sample 方法和numpy.random.random_integers 具有不同的结果,即使它们具有相同的功能?
import numpy as np
import random
random_list = []
numpy_list = []
n= 500
np_wins = 0
rand_wins = 0
for i in range(n):
rolls = random.sample(range(1,7), 3)
rand_wins += any(rolls[0] < roll for roll in rolls)
rolls = np.random.random_integers(1, 6, 3)
np_wins += any(rolls[0] < roll for roll in rolls)
print "numpy : {}".format(np_wins/(n * 1.0))
print "random : {}".format(rand_wins/(n * 1.0))
结果:
Press ENTER or type command to continue
numpy : 0.586
random : 0.688
【问题讨论】: