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";
}
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.