目标是模拟给定理论概率的实际发生次数。
例如,一个 6 面偏向骰子,落地概率 (1,2,3,4,5,6) 为 (0.1,0.2,0.15,0.25,0.1,0.2)。 掷骰子 1000 次,输出模拟得到每张面的数量。
我知道 numpy.random.choices 提供了生成每个滚动的函数,但我需要每个面的着陆次数的摘要。 上述 Python 的最佳脚本是什么?
最佳答案
Numpy 可以用来轻松且非常高效地做到这一点:
faces = np.arange(0, 6)
faceProbs = [0.1, 0.2, 0.15, 0.25, 0.1, 0.2] # Define the face probabilities
v = np.random.choice(faces, p=faceProbs, size=1000) # Roll the dice for 1000 times
counts = np.bincount(v, minlength=6) # Count the existing occurrences
prob = counts / len(v) # Compute the probability
关于python模拟给定理论概率的实际发生次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69166637/