Mea culpa (again). The results from my original function are tent shaped, except the two extreme values are half as big as they would be expected to be.
It's more likely the result of rounding slop on my part than it is an artifact of using random numbers. No such effects are seen when the function is this straightforward 2-dice method:
sub roll_dice {
my ($low_end, $range) = @_;
int(rand(int $range/2)) + int(rand(1 + $range - int $range/2)) + $lo
+w_end;
}
And here's dice-rolling code generalized to let you choose a range and number of dice, and it will figure how many sides each die needs. (No fractional dice, here.)
sub roll_dice {
my ($low_end, $range, $dice) = @_;
my $sidesneeded = $range + $dice - 1;
my $r = 0;
for (0..$dice-1) {
my $sides = int($sidesneeded / ($dice-$_));
$sidesneeded -= $sides;
$r += int(rand $sides);
}
$low_end + $r;
}
The PerlMonk tr/// Advocate
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.