Though the node title implies otherwise, it would appear that the OP isn't generating random die rolls, but calculating the average die roll from 4d6 drop the lowest.

It probably sounds like a silly sort of thing, but as a gamemaster, I have actually found myself working out what the average roll for a given group of dice is, and what the percentage chance of any given number from a given group of dice, for the purpose of crafting a random roll chart.

And here's my simplified version, I decided to stick with the nested for loops so as to stay with the methodology of the OP's code, also, instead of counting iterations, I just did 6**4, since the number of iterations won't change.

#!/usr/bin/perl use warnings; use strict; my $total; for my $i (1..6) { for my $j (1..6) { for my $k (1..6) { for my $l (1..6) { # get the sum for this dice-roll my @roll = (sort ($i, $j, $k, $l))[1..3]; $total += eval join '+', @roll ; }}}} print $total/6**4

or, if you'd prefer, as a (somewhat unwieldy) one-liner:

for $i(1..6){for $j(1..6){for $k(1..6){for $l(1..6){$total+=eval join '+',(sort($i,$j,$k,$l))[1..3]}}}}print $total/6**4

In reply to Re: Re: Rolling DND Dice. by Koosemose
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.