The Museum of HP Calculators

HP Forum Archive 20

[ Return to Index | Top of Index ]

Sort of OT: Dice odds
Message #1 Posted by Dave Britten on 12 Sept 2011, 10:08 a.m.

I'm already familiar with calculating the odds of getting a certain number of specific individual results when rolling an arbitrary number of dice (simple binomial distribution). But a fair amount of Google searching and working out numbers by hand hasn't led me to a general formula for calculating the odds of getting a certain total when rolling an arbitrary number of dice. (e.g. What are the odds of rolling a total of 17 or greater using four standard 6-sided dice?) This looks like some form of binomial expansion, but I'm drawing a blank putting it all together.

And I can always post a 15c implementation of the calculation, since that's the hot topic at the moment. ;)

      
Re: Sort of OT: Dice odds
Message #2 Posted by M. Joury on 12 Sept 2011, 10:24 a.m.,
in response to message #1 by Dave Britten

Try this page: Chance of total with 'x' dice

From the above page:

This section endeavors to answer the frequently asked question on the probability for any given total over the throw of multiple dice. ... My method was a recursive computer program. No, I don't know of an easy non-recursive formula.

Hope this helps.

Cheers,

-Marwan

            
Re: Sort of OT: Dice odds
Message #3 Posted by Dave Britten on 12 Sept 2011, 11:34 a.m.,
in response to message #2 by M. Joury

I was afraid I might have to resort to that. Now, to really complicate things, I also want to be able to do this with non-standard dice. For example, a 6-sided die with faces reading 0, 0, 0, 1, 1, 2, or something like that, and maybe even mixtures of different dice. I'll have to see how far I can take this on the 15c LE when it shows up, though honestly I think this would be easiest to implement with SQL and liberal use of cross joins.

                  
Re: Sort of OT: Dice odds
Message #4 Posted by M. Joury on 12 Sept 2011, 11:48 a.m.,
in response to message #3 by Dave Britten

SQL? If you end up going that route I would love to see how you do it. Although I would not consider myself a SQL programmer (C, C++, C#) I do program SQL on a regular basis for my job and solving this using SQL would never (never did) even occur to me!

Cheers,

-Marwan

                        
Re: Sort of OT: Dice odds
Message #5 Posted by Dave Britten on 12 Sept 2011, 12:44 p.m.,
in response to message #4 by M. Joury

Sure, here's what I slopped together for SQL Server (I'm assuming you have access to that, with C# background). The memory/CPU usage gets really out of hand with a large number of dice. Doing 7 six-sided dice takes about 10 seconds on an 8-core server with 32 GB RAM. But you can mix and match types of dice, and do non-standard dice.

http://dave.brittens.org/Dice.sql

You'll also need to have my string split function in the current database.

http://dave.brittens.org/fn_split.sql

                              
Re: Sort of OT: Dice odds
Message #6 Posted by M. Joury on 12 Sept 2011, 12:46 p.m.,
in response to message #5 by Dave Britten

Thanks Dave!

      
Re: Sort of OT: Dice odds
Message #7 Posted by Egan Ford on 12 Sept 2011, 12:22 p.m.,
in response to message #1 by Dave Britten

This smells of Project Euler. I'm afraid you are going to have to tough it out like the rest of us have. :-)

            
Re: Sort of OT: Dice odds
Message #8 Posted by Dave Britten on 12 Sept 2011, 12:32 p.m.,
in response to message #7 by Egan Ford

Ha ha, I was wondering if there might have been one on there like that. I should check my Project Euler source code and see if I solved it and forgot about it.

Actually, I'm more interested in game play and/or design at the moment, and figuring these things out mathematically could come in handy.

      
Re: Sort of OT: Dice odds
Message #9 Posted by Thomas Klemm on 12 Sept 2011, 3:34 p.m.,
in response to message #1 by Dave Britten

Quote:
What are the odds of rolling a total of 17 or greater using four standard 6-sided dice?

There's a function glob in Perl that does the cross joins. For example, this produces nine strings, one for each pairing of fruits and colors:

@many = glob "{apple,tomato,cherry}={green,yellow,red}";

That's the program I came up with:

#!/usr/bin/perl

use strict; use warnings;

my $dice = '+{1,2,3,4,5,6}'; my $n = 4;

my %seen; for my $roll (glob $dice x $n) { my $sum = eval $roll; $seen{$sum}++; };

my $total = 0; for my $value (sort { $a <=> $b } keys %seen) { printf <<EOF, $value, $seen{$value}; %4d %6d EOF $total += $seen{$value} if 17 <= $value; }

printf <<EOF, $total, $total/6**$n;

Total = %d (%.5f) EOF

And that's the result it produces when the script is started with time:

   4      1
   5      4
   6     10
   7     20
   8     35
   9     56
  10     80
  11    104
  12    125
  13    140
  14    146
  15    140
  16    125
  17    104
  18     80
  19     56
  20     35
  21     20
  22     10
  23      4
  24      1

Total = 310 (0.23920)

real 0m0.032s user 0m0.029s sys 0m0.002s

I hope it is useful and you can modify it to your needs. It's not optimized for speed (the function eval is used often), but for small n it might be fast enough.

Kind regards
Thomas

Edited: 12 Sept 2011, 5:14 p.m.

      
Re: Sort of OT: Dice odds
Message #10 Posted by Kiyoshi Akima on 13 Sept 2011, 1:29 p.m.,
in response to message #1 by Dave Britten

Here's an unoptimized, brute-force implementation on the 15C:

001 .            012 LBL 3        023 ISG 0
002 9            013 6            024 DSE 4
003 STO 0        014 STO 4        025 GTO 4
004 6            015 LBL 4        026 DSE 3
005 STO 1        016 RCL 1        027 GTO 3
006 LBL 1        017 RCL + 2      028 DSE 2
007 6            018 RCL + 3      029 GTO 2
008 STO 2        019 RCL + 4      030 DSE 1
009 LBL 2        020 1            031 GTO 1
010 6            021 7            032 RCL 0
011 STO 3        022 x<=y?        033 INT

It runs in just under ten seconds on my HHC2010 15C+. It did take a little longer to key it in on the fly...

All sorts of time optimizations are possible. For example, if you total up the first three dice, you can immediately calculate the number of ways the fourth die would push the total to 17 or above, eliminating the fourth loop.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall