Post Reply 
Dice probabilities
04-30-2024, 12:00 PM (This post was last modified: 04-30-2024 12:06 PM by Dave Britten.)
Post: #15
RE: Dice probabilities
Here's a short Python program I wrote a long time ago for generating probability tables:

https://gist.github.com/dave-britten/c4b...65ce54497b

Roughly half of it is just processing the user input! Enter one die at a time, and enter a blank line to terminate input.

EDIT: It's so old, in fact, that I just realized it's for Python 2! Here's a quick update to Python 3:

Code:
import re

dice = [] #Input dice as polynomial coefficients    

print("Enter face values as non-negative integers, separated by spaces, commas, or semicolons. (e.g. \"1,2;3 4 5 6\")")
while True:
    rawdie = [] #List of face values
    qty = 1 #Quantity of a single die spec

    inp = input("Die " + str(len(dice) + 1) + "? ")
    if inp == "":
        break
    #rawdie = [int(face) for face in inp.split(" ")]
    rawdie = [int(face.strip()) for face in re.split(" |,|;", inp) if face.strip()]
    qty = input("Quantity? [1] ")

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

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

    dice += [die for i in range(qty)]

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

lastresult = dice[0]

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

skipzero = True
p = 0.0
fcum = 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, f in enumerate(lastresult):
    fcum += f
    if f == 0 and skipzero:
        continue
    skipzero = False
    print("{:<6} {:<12} {:>10.3%} {:>10.3%} {:>10.3%}".format(i, f, f / p, (p - fcum + f) / p, fcum / p))
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Dice probabilities - dm319 - 04-29-2024, 04:23 PM
RE: Dice probabilities - Thomas Klemm - 04-29-2024, 04:55 PM
RE: Dice probabilities - Allen - 04-30-2024, 11:19 AM
RE: Dice probabilities - dm319 - 04-29-2024, 05:49 PM
RE: Dice probabilities - Thomas Klemm - 04-29-2024, 06:07 PM
RE: Dice probabilities - David Hayden - 04-30-2024, 07:30 PM
RE: Dice probabilities - Albert Chan - 04-30-2024, 08:26 PM
RE: Dice probabilities - dm319 - 04-30-2024, 09:57 PM
RE: Dice probabilities - dm319 - 04-29-2024, 07:14 PM
RE: Dice probabilities - Albert Chan - 04-29-2024, 09:22 PM
RE: Dice probabilities - dm319 - 04-30-2024, 10:07 PM
RE: Dice probabilities - Albert Chan - 05-01-2024, 11:14 AM
RE: Dice probabilities - dm319 - 04-29-2024, 07:22 PM
RE: Dice probabilities - Gil - 04-29-2024, 10:36 PM
RE: Dice probabilities - Thomas Klemm - 04-30-2024, 12:27 AM
RE: Dice probabilities - John Keith - 04-30-2024, 10:00 PM
RE: Dice probabilities - Thomas Klemm - 04-30-2024, 12:40 AM
RE: Dice probabilities - Thomas Klemm - 04-30-2024, 06:43 AM
RE: Dice probabilities - Thomas Klemm - 04-30-2024, 08:06 AM
RE: Dice probabilities - Albert Chan - 04-30-2024, 10:59 AM
RE: Dice probabilities - Dave Britten - 04-30-2024 12:00 PM
RE: Dice probabilities - Thomas Klemm - 04-30-2024, 05:11 PM
RE: Dice probabilities - Dave Britten - 04-30-2024, 05:58 PM
RE: Dice probabilities - Thomas Klemm - 04-30-2024, 06:04 PM
RE: Dice probabilities - Thomas Klemm - 04-30-2024, 06:25 PM
RE: Dice probabilities - Dave Britten - 04-30-2024, 07:24 PM
RE: Dice probabilities - Thomas Klemm - 04-30-2024, 09:10 PM
RE: Dice probabilities - Thomas Klemm - 05-01-2024, 03:32 AM
RE: Dice probabilities - Thomas Klemm - 05-01-2024, 04:26 AM
RE: Dice probabilities - Thomas Klemm - 05-01-2024, 04:41 AM
RE: Dice probabilities - John Keith - 05-01-2024, 06:56 PM
RE: Dice probabilities - Thomas Klemm - 05-01-2024, 05:54 AM
RE: Dice probabilities - Thomas Klemm - 05-01-2024, 08:45 PM
RE: Dice probabilities - John Keith - 05-02-2024, 02:46 PM
RE: Dice probabilities - Albert Chan - 05-03-2024, 01:08 PM
RE: Dice probabilities - Thomas Klemm - 05-03-2024, 01:20 AM



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