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

Replies are listed 'Best First'.
Re: Fractional dice (more not fewer)
by tye (Sage) on Jan 30, 2004 at 03:07 UTC

    Just the briefest of comments: It is my impression that the more dice you throw at once, the closer you come to a bell curve.

    - tye        

      I think the question is not how many trials you take (rolling 1 die 50000 times), but how many dice you sum at each trial (rolling 2 die 50000 times). Though more dice give you a better chance of getting a good sample, rolling 1 dice will give you, on average, a straight horizonal line, where rolling 2 dice and summing them give you something like a bell curve but too high. Rolling more dice than that at a time give you something less like a bell curve (way to high), not more. Rolling 1.2 dice, as the OP states, gives you a distrobution more like a bell curve, no matter how many trials you take.



      Code is (almost) always untested.
      http://www.justicepoetic.net/

        1 die gives you a flat-line graph (on average). 2 dice give you a tent-shaped graph (points along two straight, diagonal lines that meet at a peak in the middle). More dice give you a bell-shaped graph and the more dice you add, the closer it comes to a true "bell curve".

        A bell curve is defined by the shape, not how high or wide it is. Adding more (regular) dice makes the resulting values larger. Instead you can use balanced dice that give values from -2, -1, 0, 1, and 2, for example, or -2.5, -1.5, -0.5, 0.5, 1.5, and 2.5.

        - tye        

      You are correct: in the ordinary way of throwing dice and adding their results, 2 dice yields a linear distribution, peaking in the middle.What I programmed was different: averaging the results of 2 random floats yields what appears to be a parabolic distribution, peaking in the middle of the range.

      At this point, anybody reading who did well in probability and statistics is having a hearty laugh. Clearly it doesn't correspond well to dice, although it does give a practically useful probability distribution over a selected range (using normals, you can't be sure your results will be in your desired range).

      Inspired to produce a more bell-like result and to more accurately simulate real-life dice-throwing, I came up with a new sub. The fewer sides the dice have, the more you can throw and sum to get a result in the desired range, so I made two-sided dice (which are usually called coins):

      sub flip_coins { my ($low_end, $range) = @_; my $result = 0; $result += int(rand 2) for (2..$range); $low_end + $result; }
      Results at the extreme ends of the range are very sparse, so I like the distribution of my original better.

      The PerlMonk tr/// Advocate
        averaging the results of 2 random floats yields what appears to be a parabolic distribution

        I believe you are probably seeing a side effect of the pseudo-random sequence algorithm. If you add two (independent) uniform random distributions, you get a 'tent' distribution with straight sides. The results of adjacent calls to rand() are not as independent as one might hope.

        Simulating the situation with increasingly fine buckets by using "dice" doesn't change the shape of the graph.

        - tye