Right you are. Putting the possible combinations into play, I finally came up with a program that gets the right answer, and can be used for any number of sides (though I did not generalize it to be any number of dice, nor any number of discards -- it is possible to do so, but it's messy enough already). For 6 sided dice, it is probably working harder than the explicit method, but for 20 sided dice, it is much faster.

In the code, L is the low value for the roll. The number of distinct permutations possible varies, depending on how many times L is repeated, and the average value for the permutations also changes based on that.

my $sides = 16; my ($total, $rolls) = (0,0); foreach my $L (1..$sides) { # Start with all others = $L my $combos = 1; my $sum = $L * 3; $rolls += $combos; if ($sides > $L) { # How many repeat $L twice, with one higher? $combos = 4 * ($sides-$L); my $avg = 2 * $L + ($L+1+$sides)/2; $rolls += $combos; $sum += $avg * $combos; # How many repeat $L once, with two higher? $combos = 6 * ($sides-$L)**2; $avg = $L + ($L+1+$sides); $rolls += $combos; $sum += $avg * $combos; # How many do not repeat $L? $combos = 4 * ($sides-$L)**3; $avg = 3*($L+1+$sides)/2; $rolls += $combos; $sum += $avg * $combos; } printf "%d combos of three dice >= $L averaging %g\n", $combos, $sum +/$combos; $total += $sum; } printf "Average of $rolls rolls is %g\n", $total/$rolls; # Explicit method included for check $sum = $rolls = 0; for $i (1..$sides) { for $j (1..$sides) { for $k (1..$sides) { for $l (1..$sides) { my $min = $l; for ($i, $j, $k) { $min = $_ if $_ < $min } $sum += $i + $j + $k + $l - $min; ++$rolls; } } } } printf "Average of $rolls rolls is %g\n", $sum/$rolls;

The PerlMonk tr/// Advocate

In reply to Re^3: Rolling DND Dice. by Roy Johnson
in thread Rolling DND Dice. by grendelkhan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.