in reply to Rolling DND Dice.

Any reason you're not using Games::Die::Dice or similar?

C.

Replies are listed 'Best First'.
Re: Re: Rolling DND Dice.
by Koosemose (Pilgrim) on Feb 03, 2004 at 10:22 UTC

    Though the node title implies otherwise, it would appear that the OP isn't generating random die rolls, but calculating the average die roll from 4d6 drop the lowest.

    It probably sounds like a silly sort of thing, but as a gamemaster, I have actually found myself working out what the average roll for a given group of dice is, and what the percentage chance of any given number from a given group of dice, for the purpose of crafting a random roll chart.

    And here's my simplified version, I decided to stick with the nested for loops so as to stay with the methodology of the OP's code, also, instead of counting iterations, I just did 6**4, since the number of iterations won't change.

    #!/usr/bin/perl use warnings; use strict; my $total; for my $i (1..6) { for my $j (1..6) { for my $k (1..6) { for my $l (1..6) { # get the sum for this dice-roll my @roll = (sort ($i, $j, $k, $l))[1..3]; $total += eval join '+', @roll ; }}}} print $total/6**4

    or, if you'd prefer, as a (somewhat unwieldy) one-liner:

    for $i(1..6){for $j(1..6){for $k(1..6){for $l(1..6){$total+=eval join '+',(sort($i,$j,$k,$l))[1..3]}}}}print $total/6**4
Re: Re: Rolling DND Dice.
by nimdokk (Vicar) on Feb 03, 2004 at 10:15 UTC
    Probably to see if he could do it himself? Or maybe just felt like doing it rather than looking for a module :) I wrote something very similar except that you told it the number of dice you wanted to roll and how many sides to use. Nothing fancy, just a variation on an exercise I'd done for a Perl class.


    "Ex Libris un Peut de Tout"