#Roll of two dice. #Compute probabilites for the roll of two dice. #The value returned is an array of the cardinality #of the frequencies of the events X=i for i from #2 through 12. from pylab import * def dice_frequencies(): #generate the sample space s=[(i,j) for i in range(1,7) for j in range(1,7)] t=[sum(pair) for pair in s] h=histogram(t,bins=arange(1.5,13,1)) return h[0] #Use the cumulative distribution function to simulate 100,000 #samples from this distribution. Then obtain a histogram of the #relative frequencies and plot them side by side with the #theoretically derived probabilities. def display(): y=dice_frequencies()/36 z=cumsum(y) stem(arange(2,13),y,label='computed',linefmt='r-',markerfmt='k.') samples=[2+searchsorted(z,random()) for j in range(100000)] h=histogram(samples,bins=arange(1.5,13,1)) stem(arange(2.2,13.2,1),h[0]/100000,label='simulated',linefmt='k-',markerfmt='r.') title('PMF of sum of two dice') legend(loc='upper right') show()