Reputation: 23
I have this code so far and I don't know if it does the trick, but I'm getting too many results in 11 and 12, at 10000 repetitions the plot should be kind of symmetric. Where is the problem?
import numpy as np
import matplotlib.pyplot as plt
def roll_dice(n):
return [np.random.randint(1, 7) + np.random.randint(1, 7) for i in range(n)]
dices = roll_dice(10000)
plt.hist(dices)
Upvotes: 2
Views: 2010
Reputation: 2038
I think things become more clearly when the visualisation is made more explicit. By specifying the bin range (bins=...
), alignment of the bins (align=...
), and showing gaps between the bars in the histogram (rwidth=...
), you have more control of what's happening. The code below does exactly what your original code does (though in a slightly more elegant way):
N = int(1e5)
A = np.random.randint(low=1, high=7, size=N)
B = np.random.randint(low=1, high=7, size=N)
dice = A + B
plt.hist(dice, bins=np.arange(2, 14), align="left", rwidth=0.9)
plt.show()
Result:
Upvotes: 2
Reputation: 381
Your code works fine, the problem is the automatic binning of the histogram function. Use
plt.hist(dices, bins=11)
PS: Welcome to stackoverflow!
Upvotes: 0
Reputation: 839
Everything is fine. :) It's an nasty artifact of the binning. Specifically, try plt.hist(dices,bins=11)
and see for yourself. (without setting it yourself, it defaulted to using ten bins, and so the last bin in the hist included your variable's values corresponding to "11" and "12"...)
In general, take extra caution when using histograms (and that bins
parameter), especially for integer variables!
Upvotes: 1