Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( #3333=superdoc: 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"; }

In reply to RPG rolls by Kickstart

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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 or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2021-12-07 06:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (33 votes). Check out past polls.

    Notices?