Well I would go for a different solution to all the others here which all rely on you having pre-calculated the dice scores, OK if you are working with d6 but what if you go to d8 or d12? (giving away my AD&D heritage here :)

for (1..13){ my $chance = chance_of_2dice_score($_, 6); print "Chances of rolling a $_ on two dice is: $chance\n"; } sub chance_of_2dice_score { my $score = shift; my $dice = shift; my %valid_dice = map {$_,1} (4,6,8,10,12,20); die "Invalid dice $dice" if (! $valid_dice{$dice}); my $count = 0; for my $first (1..$dice){ for my $second (1..$dice){ ++$count if (($first + $second) == $score); } } return ($count / ($dice * $dice)); }

I think that this is clearer than the other solutions because I can see the mechanics of it. Now, does any-one have an algorithm for x dice? :-)

--
Until you've lost your reputation, you never realize what a burden it was or what freedom really is. -Margaret Mitchell


In reply to Re: Converting pascal code to perl by greenFox
in thread Converting pascal code to perl by Popcorn Dave

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.