I thought you'd be interested to see how much it condenses when you generalize it a bit. Instead of lose_one/two/three, there's @lose. Instead of three dice routines, a parameterized dice routine. More things could be turned into loops, but this covered the most glaring ones.
#!/usr/bin/perl use strict; use warnings; # # Everything here is very brute-force and hackish # as befits a combat-related program # sub probability_to_beat { my @defender_roll = sort { $b <=> $a } @{ +shift }; my @attackers = @_; my @lose = (0) x 3; # none, one, two foreach my $attacker_roll (@attackers) { my @attacker_roll = sort { $b <=> $a } @$attacker_roll; my $lost = 0; for (0..(@$attacker_roll > @defender_roll ? $#defender_roll : $#$attacker_roll)) { ++$lost if ($defender_roll[$_] >= $attacker_roll[$_]); } ++$lose[$lost]; } return @lose; } sub dice { my $n = shift; return ($n == 0) ? [[]] : [map { my $first = $_; map [@$first, $_], 1..6 } @{dice($n-1)}] ; } sub run { my @attackers = @{ +shift }; my @defenders = @{ +shift }; my $num_attackers = scalar @{ $attackers[0] }; my $num_defenders = scalar @{ $defenders[0] }; my @total_lose = (0) x 3; foreach my $defender_roll (@defenders) { my @lose = probability_to_beat( $defender_roll, @attackers ); my $total = $lose[0] + $lose[1] + $lose[2]; print join( ', ' => @$defender_roll) . "\t" . sprintf("%2.2f%%, %2.2f%%, %2.2f%%", ($lose[0] / $total) * 100, ($lose[1] / $total) * 100, ($lose[2] / $total) * 100, ) . "\n"; $total_lose[0] += $lose[0]; $total_lose[1] += $lose[1]; $total_lose[2] += $lose[2]; } my $total = $total_lose[0] + $total_lose[1] + $total_lose[2]; print "Totals:\t" . sprintf("%2.2f%%, %2.2f%%, %2.2f%%", ($total_lose[0] / $total) * 100, ($total_lose[1] / $total) * 100, ($total_lose[2] / $total) * 100, ) . "\n"; } for my $a_dice (3,2,1) { for my $d_dice (2,1) { print "$a_dice v $d_dice\n"; run(dice($a_dice), dice($d_dice)); } }

Caution: Contents may have been coded under pressure.

In reply to Re: Dice Roll Probabilities in Risk by Roy Johnson
in thread Dice Roll Probabilities in Risk by hardburn

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.