Post Reply 
Calculating odds of rolling a given total with a specified number of dice
08-24-2015, 06:56 PM
Post: #14
RE: Calculating odds of rolling a given total with a specified number of dice
I've been playing around with Pythonista, a rather nice Python IDE, on my iPad. Here's a Python version of the program, which I imagine will work on plenty more than just Pythonista:

Code:
dice = [] #Input dice as polynomial coefficients    
dice_quantity = 0 #Total number of dice

while True:
    rawdie = [] #List of face values
    qty = 1 #Quantity of a single die spec

    inp = raw_input("Die " + str(dice_quantity + 1) + "? ")
    if inp == "":
        break
    for face in inp.split(" "):
        rawdie.append(int(face))
    qty = raw_input("Quantity? [1] ")

    if qty == "":
        qty = 1
    else:
        qty = int(qty)

    die = [0] * (max(rawdie) + 1)
    for i in rawdie:
        die[i] += 1

    for i in range(qty):
        dice.append(die)

    print(str(rawdie) + " x " + str(qty))
    dice_quantity = dice_quantity + qty

lastresult = dice[0]

for current in dice[1:]:
    result = [0] * (len(lastresult) + len(current) - 1)
    for x in range(len(lastresult)):
        if lastresult[x] == 0:
            continue
        for y in range(len(current)):
            result[x + y] += lastresult[x] * current[y]
    lastresult = result

skipzero = True
p = 0.0
ft = 0.0
for i in lastresult:
    p += i

print "Permutations: {:.0f}".format(p)
print "{:<6} {:<12} {:>10} {:>10} {:>10}".format("Total","Freq","Prob", "p>=", "p<=")
for i in range(len(lastresult)):
    ft += lastresult[i]
    if lastresult[i] == 0 and skipzero:
        continue
    skipzero = False
    print "{:<6} {:<12} {:>10.3%} {:>10.3%} {:>10.3%}".format(i, lastresult[i], lastresult[i] / p, (p - ft + lastresult[i]) / p, ft / p)
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Calculating odds of rolling a given total with a specified number of dice - Dave Britten - 08-24-2015 06:56 PM



User(s) browsing this thread: 1 Guest(s)