Skip to content

Instantly share code, notes, and snippets.

@zardoru
Last active July 12, 2018 04:11
Show Gist options
  • Save zardoru/5e984f4a847eaaba77bb41d60fd98c6f to your computer and use it in GitHub Desktop.
Save zardoru/5e984f4a847eaaba77bb41d60fd98c6f to your computer and use it in GitHub Desktop.
# author: github.com/zardoru
# usage: python3 script.py dicecount dicesides
from math import floor
from scipy.misc import comb
import sys
# source:
# http://mathworld.wolfram.com/Dice.html
# equation 10
def probability(points, dice, sides):
div = 1 / (sides ** dice)
limit = floor((points - dice) / sides)
sumval = 0
for k in range(0, limit + 1):
sign = (-1) ** k
tcombination = comb(points - sides * k - 1, dice - 1)
sumval += sign * comb(dice, k) * tcombination
return sumval * div
dicecount = int(sys.argv[1])
dicesides = int(sys.argv[2])
minroll = dicecount
maxroll = dicesides * dicecount + 1
totalsum = 0
print("{:3} {:^9} {:^10}".format("sum", "chance", "at most"))
for i in range(minroll, maxroll):
value = probability(i, dicecount, dicesides) * 100
totalsum += value
print("{:3d} {:>9.6f} {:>10.6f}".format(i, value, totalsum))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment