骰子实验结果的分布 [英] Distribution of outcomes in dice experiments

查看:194
本文介绍了骰子实验结果的分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我写了一个简短的Python函数来绘制骰子实验的分布结果。它工作正常,但是当我运行例如 dice(1,5000)
dice(10,5000) dice(100,5000)的直方图显示了偏斜的分布(对6的高度偏爱)。但是,平均值显示 3.5 的预期值。我认为这可能与随机数生成有关,因此我尝试了2种方法:第一种使用 random.randint ,第二种使用代码。但是,它们提供了相似的结果。就像上限有问题。但是我不确定为什么会有这样的分布。

So I wrote a short Python function to plot distribution outcome of dice experiments. It's working fine but when I run for example dice(1,5000) or dice(10,5000) or dice(100,5000) the histograms shows a skewed distribution (high preference for 6). However, the average shows the to-be expected value of around 3.5. I thought maybe this has sth to do with the random number generation so I tried out 2 methods: 1st with random.randint and the 2nd one is as in code. However, they deliver similar results. Like there is something wrong with the upper limit. But I'm not sure why there is such a skewed distribution.

import matplotlib.pyplot as plt
import numpy as np
import random

# Throw a dice
def dice(N,n):
    result = np.zeros((n,N))
    '''
    N: number of dices
    n: number of experiment
    '''
    for i in range(n):
        for j in range(N):
            random_number = random.random()
            outcome = int(random_number * 6 + 1)
            result[i][j]=outcome
    laverage = np.mean(result)

    print('Result of throwing %d dice(s) for %d times:'%(N,n),result)
    print(laverage)
    plt.hist(np.resize(result,(N*n,1)),bins=[x for x in range(1,7)])
    plt.xlabel('Outcome')
    plt.ylabel('Number of occurences')
    plt.show()

dice(1,5000)


推荐答案

您的绘图仅显示5条-该条在数字的右侧,因此我相信 5 6 的结果已合并。如果更改为 range(1,8),则会看到更多期望值。

Your plot is only showing 5 bars - the bar is to the right of the number, so I believe the results for 5 and 6 are being combined. If you change to range(1,8) you see more of what you expect.

这篇关于骰子实验结果的分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆