#!/usr/bin/perl use CGI ':standard'; use lib '.'; require 'enumeration.pl'; #$\ = "\n"; print "Content-type: text/html\n\n"; my $N = param('N'); my $S = param('S'); exit if $N =~ /[^\d\s]/ || $S =~ /[^\d\s]/; my $sort = param('s'); open LOG, ">>", "./tabulate-dice.log"; print LOG time(), " $ENV{REMOTE_ADDR} N=$N S=$S\n" if $ENV{REMOTE_ADDR} && $ENV{REMOTE_ADDR} !~ /^192\.168/; unless (defined $N) { print < Number of -sided dice.

Sort by frequency pattern

EOM exit; } my %dice = (6 => [qw(die dice)], 2 => [qw(coin coins)], 13 => [qw(card cards)], 8 => [qw(octohedron octohedra)], 4 => [qw(tetrahedron tetrahedra)], 12 => [qw(dodecahedron dodecahedra)], 20 => [qw(icosahedron icosahedra)], ); my ($die, $dice) = @{$dice{$S} || ["$S-sided die", "$S-sided dice"]}; toobig() if $S > 26 && $N > 26 ; toobig() if $S**$N < 0; my @lines = gen_table($N, $S); @lines = sort {$b->[1] <=> $a->[1] || $a->[0] cmp $b->[0]} @lines if $sort; print ""; print $N == 1 ? "
One $die\n" : "
$N $dice\n\n"; my $gray = 0; my $pct = "%"; toobig() if grep $_->[1] < 0, @lines; for my $line (@lines) { my ($pat, $f) = @$line; my $bgcolor = $gray ? qq{bgcolor="#e3e3e3"} : ""; my $prob = $f/($S**$N) * 100; my $pi = int($prob); my $pf = $prob - $pi; my $p = sprintf (($pi ? "%.3f" : "%.3g"), $pf); $p =~ s/^0+//; $p =~ s/0+$//; $pi = " " if $pf =~ /e/; printf "
 %12s %s  %s%s %s\n", $pat, $f, $pi, $p, $pct; $gray = 1 - $gray; $pct = " "; } print "
"; sub toobig { print "

Too big

"; exit; }