in reply to Re: Fractional dice (more not fewer)
in thread Fractional dice

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

Replies are listed 'Best First'.
Re^3: Fractional dice (buckets)
by tye (Sage) on Jan 30, 2004 at 16:35 UTC
    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        

      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