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";
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|