 "be consistent" PerlMonks

### RPG rolls

by Kickstart (Pilgrim)
 on Feb 21, 2001 at 00:20 UTC ( #59756=CUFP: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
(redmist) Re: RPG rolls
by redmist (Deacon) on Feb 21, 2001 at 00:42 UTC
Steroidal RPG rolls
by gryng (Hermit) on Feb 21, 2001 at 01:23 UTC
I had a program similar that gave averages and standard deviations.

However, if you are just trying to get a feel for things then perhaps a graph would be better. So I whipped this up for you relatively quickly.

It will either do a statistical sample, like yours, or do an exaustive population count (give it 0 for the samplesize).

Enjoy:

```#!/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]);

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

Ciao,
Gryn

Create A New User
Node Status?
node history
Node Type: CUFP [id://59756]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2020-11-27 21:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?