Skip to content

Instantly share code, notes, and snippets.

@jobliz
Created January 12, 2013 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jobliz/4518367 to your computer and use it in GitHub Desktop.
Save jobliz/4518367 to your computer and use it in GitHub Desktop.
import sys
import itertools
import numpy as np
from collections import Counter
from matplotlib import pyplot as plt
def dice_prob(n):
sides = [(1,2,3,4,5,6) for x in xrange(n)]
combined = itertools.product(*sides)
sums = [sum(i) for i in combined]
pop = len(sums)
pairs = Counter(sums)
for value, times in pairs.iteritems():
pairs[value] = times * 1.0 / pop
return pairs
def graph(n):
data = dice_prob(int(n))
x = []
y = []
for value, prob in data.iteritems():
x.append(value)
y.append(prob)
minx = min(x)
maxx = max(x)
maxy = max(y)
plt.xticks(np.arange(minx, maxx+1, 1))
plt.yticks(np.arange(0.0, maxy, 0.005))
plt.scatter(x, y, color="blue")
plt.show()
if __name__ == "__main__":
graph(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment