← 返回题库
初级

骰子期望与方差

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve():
    import numpy as np
    import matplotlib.pyplot as plt
    a, b = 1, 6
    x = np.arange(a, b+1)
    p = np.ones(6)/6
    E = np.sum(x*p)
    V = np.sum((x-E)**2*p)
    print(f'E={E:.4f}(公式={(a+b)/2:.4f}),Var={V:.4f}(公式={((b-a+1)**2-1)/12:.4f})')
    plt.bar(x, p, color='steelblue', edgecolor='black')
    plt.xlabel('点数'); plt.ylabel('概率'); plt.title('骰子离散均匀分布')
    plt.xticks(x); plt.ylim(0, 0.2); plt.tight_layout(); plt.show()

示例

输入
solve()
期望输出
E=3.5000(公式=3.5000),Var=2.9167(公式=2.9167)
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码