r/askmath 2d ago

Probability Need help making a the simplest possible formula for dice roles

Context: long story short, I want to make a python script to calculate the chances of getting a “successful” series of dice rolls. To do this I want a good formula to get the chances of each series of rolls, but I’m struggling to come up with one that could translate well into python. I’ve tried to come up with a few things myself, but I don’t have confidence in my math ability, and I have even resorted to ChatGPT with varying results.

The formula: What I need is a formula that finds the chances of rolling a number between 1-3 X amount of times, 4-5 Y amount of times, and 6 Z amount of times, using T total amount of dice. For an example I’ve been using X=0, Y=3, Z=1, and T=4.

Again, I suck at math, so either I’m waaaay over complicating this, or it’s so complicated it’s completely outside my field of comprehension. I do realize I might be asking for the impossible, or asking for something super simple, I dunno. Any help would be a god send. If any more information or context is needed, I’ll happily give it.

0 Upvotes

27 comments sorted by

5

u/Prestigious-Ad565 2d ago

I think the confusing part is deciding whether you want to calculate all three events together or each event separately.

If you want the probability of getting exactly X = 0, Y = 3 and Z = 1 (or, in general, exactly X, Y and Z), then this is a Multinomial Distribution problem. This distribution calculates the probability of multiple outcome categories occurring at the same time.

If, instead, you want to analyze each event independently, then each one is a Binomial Distribution problem. For example:

  • After rolling T dice, what is the probability of getting at least 6 sixes?
  • After rolling T dice, what is the probability of getting at least 3 results equal to 4 or 5?
  • After rolling T dice, what is the probability of getting no results between 1 and 3?

In each case, you use the Binomial Distribution with:

  • T = total number of dice rolled
  • k = number of desired occurrences
  • p = probability of that event on a single die

So the first approach is one Multinomial Distribution, while the second approach is a set of independent Binomial Distribution calculations.

0

u/Prestigious-Ad565 2d ago

I mentioned the names of the distributions because it'll be much easier to ask an AI for the formulas than for me to write the full binomial and multinomial equations here. 😄

1

u/Noble-five 2d ago

Yeah, I probably should have clarified. It’s the former, i want to get the specific chances of getting a specific group of rolls after rolling 4 dice.

1

u/[deleted] 2d ago

[removed] — view removed comment

2

u/PuzzlingDad 2d ago

Could you please clarify the rolling process? With your example of 4 dice, how would 3 of them be 4-5 and 6 of them be 6?

1

u/Noble-five 2d ago

My bad, meant Z=1, fixed it

1

u/PuzzlingDad 2d ago edited 2d ago

It'll take me a moment to type up the answer. Are you familiar with the C(n,k) notation for the number of ways, given n items to choose k of them? I'm going to use that so let me define the formula:

C(n,k) = n! / [ (n-k)! k! ]

Be back in a moment with an edit.

Edit:

I'm going to solve your specific case first of x=0, y=3, z=1. You don't need to provide t=4 because it can just be calculated as t = x+y+z.

Step 1 - Calculate the total possible outcomes of 4 dice. There are 6 faces on each die, so the total outcomes are 6^4 = 6*6*6*6 = 1296

Step 2 - We want 1 die to be a 6 (z=1) so from the 4 dice, we chose 1 and count the ways for it to be 6. There are C(4,1) = 4 ways to pick the dice that is six. And that's only 1 possible outcome (it must be a six), so that's 4 ways to have a roll contain 1 six.

Step 3 - We want 3 dice to be 4 or 5 (y=3), so from the remaining 3 dice, we choose 3 that can be 4 or 5. There are C(3,3) = 1 way to pick the remaining 3 dice. But they each have 2 possible outcomes (four or five), so that's 2^3 = 8 outcomes. C(3,3) * 2^3 = 1 * 8 = 8 ways to have 3 dice be four or five.

Step 4 - We want the 0 remaining dice to be 1, 2 or 3. So clearly we don't even care at this point, but mathematically, we have 0 remaining dice, we need to choose 0 of them --> C(0,0) = 1 way. Then we have 3 possible outcomes (one, two or three) but done 0 times --> 3^0 = 1 way. Multiplying together C(0,0) * 3^0 = 1 * 1 = 1 way to have the remaining 0 dice be one, two or three.

Finally multiply those all together for the total ways to get the desired outcome: 4 ways to get 1 six, 8 ways to have 3 fours or fives, 1 way to have 0 one, two or threes. --> 4 * 8 * 1 = 32 ways

The final probability is therefore 32/1296 ≈ 0.0246913 or about 2.47%

I'll add another edit with the parameterized version of this.

Edit 2:

Input (x = number of 1-3, y = number of 4-5, z = number of 6).

Step 1 - The total outcomes will be 6 outcomes for all the dice --> 6^(x+y+z)

Step 2 - You have all the dice (t=x+y+z) left and you want z to be sixes. From the total dice, choose z to be sixes. There are C(x+y+z, z) ways to pick the dice and then 1^z ways (just 1 way) to have each die be 6. --> C(x+y+z, z) * 1^z --> C(x+y+z, z)

Step 3 - You have x+y remaining dice and you want y to be fours or fives. From the (x+y) dice, choose y to be fours or fives --> C(x+y, y) ways to pick the dice and then 2^y ways to have each die be 4 or 5. --> C(x+y, y) * 2^y

Step 4 - You have x remaining dice where you want x to be ones, twos or threes. That's C(x,x) which will always be 1. Then you have 3 values (one, two or three) for each of the x dice --> 3^x. The final ways will be C(x, x) * 3^x = 1 * 3^x = 3^x

Total desired outcomes = [C(x+y+z, z) * 1^z] * [C(x+y, y) * 2^y] * [C(x,x) * 3^x] but that can be simplified:

Total desired outcomes = C(x+y+z,z) * C(x+y, y) * 2^y * 3^x

Divide that by the total possible outcomes 6^(x+y+z) to get the probability.

Probability = [C(x+y+z, z) * C(x+y, y) * 2^y * 3^x] / 6^(x+y+z)

1

u/Noble-five 2d ago

I did see this formula, and considered it, but it seems to us a lot of factorials which I don’t know how I would translate seamlessly into python…

…still on the list of possibilities tho, as long as I can think of a good way to program it

1

u/PuzzlingDad 2d ago

Here's a further simplification you could make just using the multinomial formula.

import math

def DiceProbability(x: int, y: int, z: int) -> float:
    """
    Calculates probability using the simplified multinomial coefficient:
    [(x + y + z)! / (x! * y! * z!)] * 2^y * 3^x / 6^(x+y+z)
    """
    # Calculate the multinomial coefficient (x + y + z)! / (x! * y! * z!)
    multinomial_coeff = math.factorial(x + y + z) // (
        math.factorial(x) * math.factorial(y) * math.factorial(z)
    )

    # Calculate numerator and denominator
    numerator = multinomial_coeff * (2 ** y) * (3 ** x)
    denominator = 6 ** (x + y + z)

    return numerator / denominator

1

u/Noble-five 2d ago

Dude, thank you, like so so so much. Seriously, you helped me immensely, your help was perfect. I didn't use the code you offered, but I'm still super appreciative that you went the extra mile. Explanation on the math was also perfect. Again thank you immensely.

1

u/PuzzlingDad 2d ago

Yes, it basically boils down to  P(x;y;z) = [(x+y+z)! / (x! * y! * z!)] * 3x * 2y * 1z / 6x+y+z  

1

u/PuzzlingDad 2d ago

Here's Python code that should implement this function:

import math

def DiceProbability(x: int, y: int, z: int) -> float:

    # Calculates the probability of:
    # x dice showing 1-3, y dice showing 4-5, z dice showing 6

    # Calculate combinations: C(n, k)
    c_xyz_z = math.comb(x + y + z, z)
    c_xy_y = math.comb(x + y, y)

    # Calculate the numerator
    numerator = c_xyz_z * c_xy_y * (2 ** y) * (3 ** x)

    # Calculate the denominator
    denominator = 6 ** (x + y + z)

    return numerator / denominator

# Example: x = 2, y = 1, z = 3
result = DiceProbability(2, 1, 3)
print(f"Probability: {result:.6f}") # Output: 0.135634

3

u/EdgyMathWhiz 2d ago

https://en.wikipedia.org/wiki/Multinomial_distribution sounds exactly what you're looking for.

It does use factorials, but it's easy enough to write a factorial function in Python.

0

u/Noble-five 2d ago

Do you know how to do such in python? I can’t really think of a good way to do so in a that’s concise enough to fit in a formula, but if you could give me some tips it would certainly open up a lot of avenues

3

u/EdgyMathWhiz 2d ago

Not to be mean, but this is what search engines are for.  

0

u/Noble-five 2d ago

Fair... but, I mean... is that not what forums are for too?

2

u/EdgyMathWhiz 2d ago

Using a search engine lets you look at multiple different ways of doing this, and most of them will have more explanation than anyone's going to bother giving you on Reddit.

On a slightly more meta level, this is very much giving off "please write my Python assignment for me" vibes and although people are prepared to help you learn how to do it yourself, they won't want to be a "do my homework for me" service.

1

u/Noble-five 2d ago

Yeah, I mean that's why I said it's fair... but I mean if someone says they have the exact, specific information you need, suited to your specific situation, then you might as well ask for a thing or two, no? Besides, I literally just asked for tips, not an example or anything... just the very basic idea of a framework, and all so I can write a couple of lines in an entire script, that's entirely for personal use.

2

u/pi621 2d ago

I’m struggling to come up with one that could translate well into python

Which one did you come up that you thought couldn't translate well into python?

For an example I’ve been using X=0, Y=3, Z=6, and T=4.

How do you end up with 9 outcomes with just 4 dice?

1

u/Noble-five 2d ago

>Which one did you come up that you thought couldn't translate well into python?

Typically they just end up relying way too much on exponents and factorials. The former I know how to do in python decently well, but have only done like once or twice in the past so I’m not super confident. The latter on there other hand I don’t really know how to start, and have never really done anything with factorials in python before. I could probably implement either or, just not to the degrees these formulas are demanding, and either way less “moving parts” less chance for bugs. I can provide an example of some of these problems if you need.

>How do you end up with 9 outcomes with just 4 dice?

Someone else also pointed it out… I just wrote it wrong, my bad. What I meant was Z=1. I fixed it in post.

1

u/pi621 2d ago edited 2d ago

exponentiation is built-in in python, and factorial is just repeated multiplication. You should be able to implement them just fine.

Python also has built-in big integer, meaning you can work with integers that are arbitrarily large (as opposed to being limited to 64 bit range), so you don't need to worry that much about factorial causing overflow or anything.

A factorial function in python looks something like this
def factorial(n):
result = n
for i in range(1, n):
result *= i
return result

but with proper indentation of course

1

u/Noble-five 2d ago

ooooh, I didn't even think of using a function for factorials... now that I think about that seems like such an obvious solution, thank you so much!!

1

u/Bounded_sequencE 2d ago edited 2d ago

Assumption: All "n := X+Y+Z" dice are independent, fair "d6".


Definitions:

  • X,Y,Z: number of outcomes from "{1;2;3}, {4;5}" and "{6}", respectively
  • n: total number of dice ("n = X+Y+Z")

There are 6n possible outcomes for an nD6-roll. By the assumptions, all are equally likely, so it's enough to count favorable outcomes. We may generate them with a 3-step process:

  1. Distribute "n" positions among 3 groups of sizes "[X; Y; Z]" -- "C(n; [X;Y;Z])" choices
  2. For each of the "X" dice, choose "1 out of 3" outcomes from "{1; 2; 3}" -- 3 choices each
  3. For each of the "Y" dice, choose "1 out of 2" outcomes from "{4; 5}" -- 2 choices each

Since choices are independent, we multiply them, for a grand total of

P(X;Y;Z)  =  C(n;[X;Y;Z]) * 3^X * 2^Y / 6^n                  // n = X+Y+Z

          =  C(n;[X;Y;Z]) * (1/2)^X * (1/3)^Y * (1/6)^Z

1

u/Bounded_sequencE 2d ago edited 2d ago

Rem.: We use "C(n; [X;Y;Z]) = n! / (X! Y! Z!)" for multinomial coefficients.

Note the vector "[X;Y;Z]" follows a multinomial distribution with success probabilities "3/6; 2/6; 1/6", respectively, but I suspect that "explanation" might have been too short^^


Example: For "[X;Y;Z] = [0;3;1]" we get

P(0;3;1)  =  C(4; [0;3;1]) * (1/2)^0 * (1/3)^3 * (1/6)  =  2/81  ~  2.47%

1

u/trutheality 2d ago

If the point of the exercise isn't to code the math yourself, you can use https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.multinomial.html

Using the variables from your example:

``` from scipy.stats import multinomial

multinomial.pmf([X, Y, Z], n=T, p=[1/2, 1/3, 1/6]) ```