Anyone who has played tabletop games like Warhammer or Axis and Allies knows that frequently you have to roll massive numbers of dice. It can be a pain to count up what amounts to the frequency distribution of the dice you've rolled to figure the combat results. Inspired by a recent Perlmonks thread two dice question I came up with this code that takes as input the number of six-sided die rolls you want and outputs the results of the rolls and then the frequency distribution for each side 1 through six. A more readable version of the code can be found here. (The line "my $dicenum =
#Produces specified number of six-side die roll results
#then produces a frequency distribution for calculating combat results
use strict;
use Statistics::Descriptive;
my $stat = Statistics::Descriptive::Full->new();
print "How many dice do you want to roll(N)?";
my $dicenum =;
my @dicearray;
for (my $i = 0; $i < $dicenum; $i++) { $dicearray[$i] = (int(rand(6)) + 1); my $roll = $i+1; print "Roll ".$roll." result is: ".$dicearray[$i]; print "\n"; } print "\n"; sleep 3; my @bins = (1,2,3,4,5,6); $stat->add_data(\@dicearray);
my %f = $stat->frequency_distribution(\@bins);
for (sort {$a <=> $b} keys %f) {
print "Result = $_, count = $f{$_}\n";
}
frequency distribution calculator
No comments:
Post a Comment