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 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.
#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 = <STDIN>;
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";
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.