http://qs321.pair.com?node_id=59756

A friend of mine has written a pen-and-paper RPG based on the principle of multiple d6 rolls, taking the highest two dice. Of course, the higher the number of dice in the pool, the steeper the bell curve towards the top end of the scale. However, he had no way of doing a few million rolls to see how this affected the probabilities. This script gives him some stats he can graph to get a better idea.

#!/usr/bin/perl ################################################################### # Script to do some statistics (creating bell curves for JD's RPG) ################################################################### $iterations = 1000000; # How many rolls do we make? $dicemax = 6; # 6=d6, 8=d8, etc. $numdice = 3; # How many dice in the pool? 3 = 3d$dicemax $dicekeep = 2; # How many dice to keep/use? ################################################################### $maxtotal = $dicemax * $dicekeep; # currently unused srand (time ^ $$); for (1..$iterations) { for (1..$numdice) { push(@set, int(rand($dicemax))+1); } @set = reverse sort(@set); # to get the highest dice out front $total = 0; for (0..($dicekeep-1)) { $total = $total + @set[$_]; } push(@alltotals, $total); # $total is the total of the top dic +e ($dicekeep), usually 2 @set = undef; } foreach $topscore (@alltotals) { $scoreset{$topscore}++; } sub mysort { $a <=> $b; } @keys = sort mysort (keys %scoreset); foreach $key (@keys) { print "$key: $scoreset{$key}\n"; }