If you roll one die many times, you wind up with a uniform distribution of results. If you roll a pair of dice many times, you get a lot more results in the midrange than on the extremes. What if you want something in-between? A bell curve, but not quite so tall (that is, not quite so center-weighted)?

Obviously, you roll 1.5 dice. Or 1.2 dice. The sub below allows you to roll any positive number (*) of dice. I've included a little wrapper program so you can see the distribution of results of 50000 throws.

The parameters are, in order: lowest value to return (integer), number of distinct possible return values (integer), how many dice to use.

* numbers smaller than 1 all act like 1

my $range=12; my $low_end=6; my $dice=1.2; my %freq = (); for (1..50000) { $freq{ roll_dice($low_end, $range, $dice) }++ } print "$_: $freq{$_}\n" for sort {$a<=>$b} keys %freq; sub roll_dice { my ($low_end, $range, $dice) = @_; my $result = 0; my $i; for ($i = $dice; $i >=1; --$i) { $result += rand($range); } $result += rand($range) * $i; int($low_end) + int($result/$dice); }

In reply to Fractional dice by Roy Johnson

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.