Python 模拟掷骰子

2021-03-19  本文已影响0人  水之心

代码:

import numpy as np
from matplotlib import pyplot as plt

fair_probs = [1.0 / 6] * 6
# 进行500组实验,每组抽取10个样本
counts = np.random.multinomial(10, fair_probs, size=500)
cum_counts = counts.astype(np.float32).cumsum(axis=0)
estimates = cum_counts / cum_counts.sum(axis=1, keepdims=True)

plt.rcParams['figure.figsize'] = ((6, 4.5))
for i in range(6):
    plt.plot(estimates[:, i],
             label=("P(die=" + str(i + 1) + ")"))
plt.axhline(y=0.167, color='black', linestyle='dashed')
plt.gca().set_xlabel('Groups of experiments')
plt.gca().set_ylabel('Estimated probability')
plt.legend()
plt.show()

输出:

上一篇 下一篇

猜你喜欢

热点阅读