Distribution of outcomes in dice experiments

前端 未结 3 2227
礼貌的吻别
礼貌的吻别 2020-12-21 06:34

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

3条回答
  •  执笔经年
    2020-12-21 06:55

    If you are lazy (like me), you can also use numpy to directly generate a matrix and seaborn to deal with bins for you:

    import numpy as np
    import seaborn as sns
    
    dices = 1000
    throws = 5000
    x = np.random.randint(6, size=(dices, throws)) + 1
    sns.distplot(x)
    

    Which gives:

    Seaborn usually make good choices, which can save a bit of time in configuration. That's worth a try at least. You can also use the kde=False option on the seaborn plot to get rid of the density estimate.

    Just for the sake of it and to show how seaborn behave, the same with the sum over 100 dices:

    dices = 100
    throws = 5000
    x = np.random.randint(6, size=(dices, throws)) + 1
    sns.distplot(x.sum(axis=0), kde=False)
    

提交回复
热议问题