#!/usr/bin/perl -w use strict; my $dicemax = shift @ARGV || 6; my $numdice = shift @ARGV || 6; my $dicekeep = shift @ARGV || 6; my $samplesize = shift @ARGV || 0; my $totstates = $dicemax ** $numdice; my $min = $dicekeep; my $max = $dicekeep * $dicemax; my $num = $max - $min + 1; my @tot; @tot[$min..$max] = map {0} ($min..$max); if ($samplesize) { srand; for my $x (1..$samplesize) { my $sum=0; $sum += $_ for (sort {$b<=>$a} map {int rand($dicemax)+1} (1..$numdice) )[0..$dicekeep-1]; $tot[$sum]++; } $totstates = $samplesize; } else { my $count = 0; my @dice = map {1} (1..$numdice); while ($count++ < $totstates) { my $x = 0; $dice[$x]++; while ($dice[$x] > $dicemax) { $dice[$x++] = 1; $dice[$x]++; } my $sum=0; $sum += $_ for (sort {$b<=>$a} @dice)[0..$dicekeep-1]; $tot[$sum]++; } } my @per; @per[$min..$max] = map {$_/$totstates*100} @tot[$min..$max]; my $maxper = (sort {$b<=>$a} @per[$min..$max])[0]; print "Roll Percent| Frequency Graph (0 - $maxper%)\n"; for my $x ($min..$max) { my $c = ($num-$x+$min)/$num*$maxper; printf "%3d %5.2f | ", $x, $per[$x]; do { print $per[$_] > $c ? "*" : " " for ($min..$max) } if $max-$min < 75; print "\n"; } print "-"x(16+$max-$min),"\n"; for my $x (100,10,1) { print " "x14,map {int $_ / $x % 10} ($min..$max); print "\n"; }