美文网首页
Python 模拟掷骰子

Python 模拟掷骰子

作者: 水之心 | 来源:发表于2021-03-19 10:26 被阅读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()

输出:

相关文章

网友评论

      本文标题:Python 模拟掷骰子

      本文链接:https://www.haomeiwen.com/subject/lmhvcltx.html