in reply to Rolling DND Dice.

Well, this is a little longer than one line!

If you don't want to think too hard, you can find the expected value experimentally -- Just do a million (or so) status rolls see what the average is. Note the array-slice of the sort, which finds the 3 highest dice with much less mucky-muck.

use List::Util 'sum'; my ($iter, $sum) = shift || 1_000_000; $sum += sum +(sort map {1 + int rand 6} 1..4)[1..3] for 1..$iter; print $sum/$iter, $/;
For a million iterations, I got an expected value of 12.244657, which is within 0.0005% of your exact answer.

You could also use some of the Die/Dice modules on CPAN, but rolling (fair) dice is pretty trivial to do yourself.

blokhead

Replies are listed 'Best First'.
Re: Re: Rolling DND Dice.
by grendelkhan (Sexton) on Feb 04, 2004 at 03:08 UTC
    See, I was going to use sort to get the highest three values, but sort slaps together repeated values, so that sort(1,3,3,7) gives (1,3,7). Is there some way around this? I think there's a pragma to make sort use a stable method (which would have the side effect of not deleting dupes?), but it's in 5.8, and I'm using 5.6 here. (Old, old distribution which desperately needs an upgrade.)
      Huh?
      $ perl -e 'print join " ", sort 1,1,1,3,3,7' 1 1 1 3 3 7
      Unstable sorting means input order isn't preserved for ties, not that stuff is removed. By your reasoning, sort { 0 } @array would just return one element every time, since all the items are tied in sorting order.

      blokhead