Calculating odds of rolling a given total with a specified number of dice - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: Not HP Calculators (/forum-7.html) +--- Forum: Not remotely HP Calculators (/forum-9.html) +--- Thread: Calculating odds of rolling a given total with a specified number of dice (/thread-4161.html) |
Calculating odds of rolling a given total with a specified number of dice - Dave Britten - 06-15-2015 03:25 PM I could swear I had a direct formula for this at one point, but I'm totally drawing a blank. Suppose I've got n standard 6-sided dice, numbered 1 through 6. Is there a formula for calculating the frequency (i.e. number of different permutations) of rolling a given total? For example, what are the odds of rolling a total of 22 on 9 dice? (It's about 1.45% in this case.) The brute-force approach is really simple on SQL Server (code below), and runs all ~10 million permutations in about 1-2 seconds on the server I've got access to, but my HP 48 probably won't be so quick. Code: WITH die AS ( And for extra fun, I'm also wondering if such a formula could be extended to rolling a pool of non-standard, mixed dice, e.g. maybe three of them numbered 0, 0, 0, 1, 2, 3, two numbered 0, 0, 1, 2, 2, 3, and one with 0, 0, 1, 2, 3, 4. The brute-force approach can be adapted for this trivially. The motivation behind this would be in case I'm playing a board game and want to quickly math out my odds of success before diving into a potentially pivotal move. RE: Calculating odds of rolling a given total with a specified number of dice - Thomas Klemm - 06-15-2015 06:59 PM (06-15-2015 03:25 PM)Dave Britten Wrote: For example, what are the odds of rolling a total of 22 on 9 dice? (It's about 1.45% in this case.) It's the coefficient of \(x^{22}\) in: \( (\frac{x^6}{6}+\frac{x^5}{6}+\frac{x^4}{6}+\frac{x^3}{6}+\frac{x^2}{6}+\frac{x}{6})^9 \) The exact value is: \(\frac{16211}{1119744}\) Cheers Thomas RE: Calculating odds of rolling a given total with a specified number of dice - Dave Britten - 06-15-2015 08:14 PM (06-15-2015 06:59 PM)Thomas Klemm Wrote:(06-15-2015 03:25 PM)Dave Britten Wrote: For example, what are the odds of rolling a total of 22 on 9 dice? (It's about 1.45% in this case.) That makes sense. What's the practical way to do a multinomial expansion like that, aside from feeding it through a CAS and letting it sum like terms? Seems like you'd have to find all permutations of 9 integers between 1 and 6 that add up to, e.g., 22, then you're back to square one. RE: Calculating odds of rolling a given total with a specified number of dice - Thomas Klemm - 06-15-2015 10:02 PM (06-15-2015 08:14 PM)Dave Britten Wrote: What's the practical way to do a multinomial expansion like that, aside from feeding it through a CAS and letting it sum like terms? You could use matrix multiplication to multiply two polynomials: \(\begin{bmatrix} 1\\ 1\\ 1\\ 1\\ 1\\ 1 \end{bmatrix}\cdot\begin{bmatrix} 1 & 1 & 1 & 1 & 1 & 1 \end{bmatrix}=\begin{bmatrix} 1 & 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 & 1 \\ \end{bmatrix}\) At least the HP-42S allows to grow the matrix at the bottom: \(\begin{bmatrix} 1 & 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ \end{bmatrix}\) Then you can transpose it: \(\begin{bmatrix} 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{bmatrix}\) Now when you shrink it just by the size of the 2nd matrix we get this: \(\begin{bmatrix} 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 1 \\ \end{bmatrix}\) To add up all the columns we can use again matrix multiplication: \(\begin{bmatrix} 1 & 1 & 1 & 1 & 1 & 1 \end{bmatrix}\cdot\begin{bmatrix} 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 1 \\ \end{bmatrix}=\begin{bmatrix} 1 & 2 & 3 & 4 & 5 & 6 & 5 & 4 & 3 & 2 & 1 \end{bmatrix}\) Now you can repeat this to calculate \(\begin{bmatrix} 1 & 1 & 1 & 1 & 1 & 1 \end{bmatrix}^4\) and \(\begin{bmatrix} 1 & 1 & 1 & 1 & 1 & 1 \end{bmatrix}^8\). A final multiplication by \(\begin{bmatrix} 1 & 1 & 1 & 1 & 1 & 1 \end{bmatrix}\) will give you the desired list of coefficients: CoefficientList[(1 + x + x^2 + x^3 + x^4 + x^5)^9, x] Cheers Thomas RE: Calculating odds of rolling a given total with a specified number of dice - Thomas Klemm - 06-15-2015 11:52 PM It's much easier to use lists: Code: \<< DUP Start with this list: { 1 1 1 1 1 1 } Repeat the program above 8 times. To get the coefficient of \(x^{22}\) use: 14 GET That's because 14 = 22 - 9 + 1 and the list is symmetric. You should get: 145,899 Divide this by 69 = 10,077,696 to get the desired probability. HTH Thomas RE: Calculating odds of rolling a given total with a specified number of dice - Dave Britten - 06-16-2015 12:06 AM (06-15-2015 11:52 PM)Thomas Klemm Wrote: It's much easier to use lists: Thanks Thomas, that looks like a good trick for dealing with pools of standard six-sided dice. Not sure if that can be applied to multiplication of arbitrary polynomials, i.e. pools of non-uniform, non-standard dice with any number of sides. I may end up having to use the matrix approach there and just multiply the polynomials, collect terms, etc. RE: Calculating odds of rolling a given total with a specified number of dice - Thomas Klemm - 06-16-2015 12:31 AM Another approach could be to use: \(x^5+x^4+x^3+x^2+x+1=\frac{x^6-1}{x-1}\) if \(x\ne 0\). Now we can use the binomial formulae to calculate the coefficients of \(x^k\) for both expressions \((x^6-1)^9\) and \((x-1)^9\) and just have to implement polynomial division. Cheers Thomas RE: Calculating odds of rolling a given total with a specified number of dice - Thomas Klemm - 06-16-2015 09:54 AM (06-15-2015 03:25 PM)Dave Britten Wrote: And for extra fun, I'm also wondering if such a formula could be extended to rolling a pool of non-standard, mixed dice, e.g. maybe three of them numbered 0, 0, 0, 1, 2, 3, two numbered 0, 0, 1, 2, 2, 3, and one with 0, 0, 1, 2, 3, 4. CoefficientList[(3+x+x^2+x^3)^3 (2+x+2 x^2+x^3)^2 (2+x+x^2+x^3+x^4), x] In this case it appears that 7 is the most probable total. Cheers Thomas RE: Calculating odds of rolling a given total with a specified number of dice - Dave Britten - 06-16-2015 12:31 PM Looks like there's a ton of options for multinomial expansion if you're raising a single multinomial to a power. I'll probably have to take the direct polynomial multiplication approach, though, since I want to handle arbitrary numbers of dice which may be non-uniform and non-standard. That should be a hell of a lot more efficient than summing up every permutation, though. RE: Calculating odds of rolling a given total with a specified number of dice - Thomas Klemm - 06-16-2015 01:05 PM (06-16-2015 12:31 PM)Dave Britten Wrote: I'll probably have to take the direct polynomial multiplication approach, though, since I want to handle arbitrary numbers of dice which may be non-uniform and non-standard. You might consider using FFT as it is a built-in function. Polynomial Multiplication and Fast Fourier Transform Cheers Thomas RE: Calculating odds of rolling a given total with a specified number of dice - Thomas Klemm - 06-16-2015 02:10 PM Just tested it with HP-48GX. You start with a 64-dimensional vector: [0, 0, … , 0 , 0, 1, 1, 1, 1, 1, 1] Run the discrete Fourier transform: FFT Transmogrify the result to a list of 64 complex numbers: OBJ→ OBJ→ DROP →LIST { (6,0) (5.5701807… } Apply the 9th power (point-wise): 9 yx { (10077696,0) (-8… } Transmogrify back to a vector: OBJ→ { } + →ARRY Compute the inverse Fourier transform: IFFT [ (.00000034,0) (.… ] Drawback is that you have now an array of complex numbers that are nearly real. Thus transmogrify back to a list and run: ABS 0 RND And finally you have the list of coefficients: { 0 0 0 0 0 0 0 0 0 0 1 9 45 165 495 1287 2994 6354 12465 22825 39303 Of course the list is a little longer than just that. Cheers Thomas RE: Calculating odds of rolling a given total with a specified number of dice - Dave Britten - 06-16-2015 03:00 PM (06-16-2015 02:10 PM)Thomas Klemm Wrote: Just tested it with HP-48GX. Very interesting stuff. I have to admit, I haven't done anything with FFTs before (though I have a general idea of what they're used for), so this is a bit out of my depth. However, a simple C# version on my desktop gives results for 9 standard 6-sided (1-6) dice nearly instantaneously. The code is below; I've avoided heavily optimizing the array use to keep the indexing simple and make sure it's working correctly first, so there's a lot of zero padding for the lower powers of x. Note that results aren't necessarily symmetrical for for "unusual" die pools, so it can't just compute up to the halfway mark. I should be able to do a C version for my 200LX, though the input parsing and mallocing will be a bit more labor-intensive. Everything else is just simple looping and arithmetic. I have a feeling trying to handle this with nested DOLISTs on a 48 would be fairly slow, but I might give it a shot. Code: class Program { Usage: Run the program, enter the values of the faces of each die, separated with spaces, commas, or semicolons. For example, "1 2 3 4 5 6", or "0 0 0 1 2 3". Enter the quantity after each given die, or press enter for the default of 1. When finished, enter a blank line when prompted for the next die faces. Sample output: Code: Die 1? 0 0 0 1 2 3 RE: Calculating odds of rolling a given total with a specified number of dice - Thomas Klemm - 06-16-2015 04:48 PM (06-16-2015 03:00 PM)Dave Britten Wrote: Very interesting stuff. I have to admit, I haven't done anything with FFTs before (though I have a general idea of what they're used for), so this is a bit out of my depth. This is the relevant Convolution Theorem: It's similar to how we avoid the multiplication with: \(\log(a\cdot b) = \log(a) + \log(b)\). Code: %%HP: T(3)A(D)F(.); With these two programs you can now run: A TO 3 yx B TO x2 × C TO × FRO { 216 540 1314 2393 3618 4880 5774 6112 5878 5116 4022 2886 1870 1080 562 256 98 32 8 1 0 0 0 0 0 0 0 0 0 0 0 0 } HTH Thomas RE: Calculating odds of rolling a given total with a specified number of dice - Dave Britten - 08-24-2015 06:56 PM 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 |