in reply to Converting pascal code to perl
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? :-)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)); }
--
Until you've lost your reputation, you never realize what a burden it was or what freedom really is. -Margaret Mitchell
|
|---|