python 给定理论概率模拟实际发生次数

python simulation of actual number of occurrence given theoretical probabilities

目标是模拟给定理论概率的实际发生次数。

例如,一个 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

不用 Numpy 也可以完成。

import random
random.choices([1,2,3,4,5,6], weights=[0.1,0.2,0.15,0.25,0.1,0.2], k=1000)