Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import random
- from matplotlib import pyplot as plt
- def random_sum(nlist=[6,8,8]):
- domainlist = [[1+x for x in range(n)] for n in nlist]
- choicelist = [random.choice(domain) for domain in domainlist]
- return sum(choicelist)
- def trial(nlist=[6,8,8], num_samples=1000):
- data = []
- for n in range(num_samples):
- data += [random_sum(nlist=nlist)]
- if n%100000==0:
- print str(float(n)/num_samples*100)+'% complete'
- plt.xlim([min(data)-0.5, max(data)+0.5])
- bins = [1.5+n for n in range(sum(nlist))]
- plt.hist(data, bins=bins, alpha=0.5, normed=True)
- plt.title('{0} rolls with dice of sides {1}'.format(*[num_samples, nlist]))
- plt.xlabel('sum')
- plt.ylabel('frequency')
- plt.show()
Add Comment
Please, Sign In to add comment