Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Six-side dice roll calculator

by Scott7477 (Chaplain)
on Jul 19, 2007 at 18:31 UTC ( #627592=CUFP: print w/replies, xml ) Need Help??

Anyone who has played tabletop games like Warhammer or Axis and Allies knows that frequently you have to roll massive numbers of dice. It can be a pain to count up what amounts to the frequency distribution of the dice you've rolled to figure the combat results. Inspired by a recent thread two dice question I came up with this code that takes as input the number of six-sided die rolls you want and outputs the results of the rolls and then the frequency distribution for each side 1 through six.

#Produces specified number of six-side die roll results #then produces a frequency distribution for calculating combat results use strict; use Statistics::Descriptive; my $stat = Statistics::Descriptive::Full->new(); print "How many dice do you want to roll(N)?"; my $dicenum = <STDIN>; my @dicearray; for (my $i = 0; $i < $dicenum; $i++) { $dicearray[$i] = (int(rand(6)) + 1); my $roll = $i+1; print "Roll ".$roll." result is: ".$dicearray[$i]; print "\n"; } print "\n"; sleep 3; my @bins = (1,2,3,4,5,6); $stat->add_data(\@dicearray); my %f = $stat->frequency_distribution(\@bins); for (sort {$a <=> $b} keys %f) { print "Result = $_, count = $f{$_}\n"; }

Replies are listed 'Best First'.
Re: Six-side dice roll calculator
by CountZero (Bishop) on Jul 19, 2007 at 19:54 UTC
    It takes all the fun out of these games and does not allow me to show off my collection of dice!! ;-)

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Six-side dice roll calculator
by MonkE (Hermit) on Jul 19, 2007 at 22:46 UTC
    Cool, but why limit it to six-sided dice, when you could use N-sided dice: 4, 6, 8, 10, 12, and 20-sided. That might appeal to players of other RPG's. It would only require a small change to your program.
      Adding the functionality for N-sided dice was next on my list; needless to say it should be relatively trivial to implement. After looking through CPAN, it appears that David Cantrell's Games::Dice::Advanced module also fills the bill.

      Also, as zentara points out further down this thread, davido's Custom-faced Dice snippet allows you to "define the faces of the die. That allows for traditional six sided numeric-faced dice, or alphabet-faced dice, or even, magic-eight-ball style dice."

      I scanned the source code of the various dice modules at CPAN, and one thing I will say for my implementation here is that it appears to be unique in using the Statistics::Descriptive module. I found the documentation for this module to be very well written and it includes most of the basic stats functions that one might use regularly. The module's interface is completely object oriented, which I prefer. I would recommend that anyone who does stats with Perl take a look at this module.
      Don't forget the 2 sided dice also known as coins.

      Well, strictly speaking it is 3 sided, but it is very, very rare for a coin to come up standing on its side.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Six-side dice roll calculator
by rpanman (Scribe) on Jul 20, 2007 at 10:21 UTC
    Thought I'd dump my script here too! Input lines in 3d6, 1d20, 2d4 format :-)

    It even support the wonderful d3 format for those side of the coin landings!
    #!/usr/bin/perl use strict; use warnings; while (<>){ next unless my ($number, $sides) = ($_ =~ /^(\d+)d(\d+)$/); my $total = 0; for (1..$number){ my $roll = int(rand($sides)+1); print "$roll "; $total += $roll; } print "($total)\number"; }
Re: Six-side dice roll calculator
by Tux (Canon) on Jul 20, 2007 at 07:28 UTC

    I once forgot my dice on holiday, but I fortunatly had my laptop with me, so the kids could be made happy again. No need to understand the dutch comment I think:

    #!/pro/bin/perl use strict; use warnings; # usage: dobbelsteen [ n [ s ]] # n : aantal stenen (1) # s : zijden per steen (6) my $n = shift (@ARGV) // 1; my $s = shift (@ARGV) // 6; $, = " "; srand time; while (<>) { print "worp: ", (map { 1 + int rand $s } 1 .. $n), "\n"; }

    This version is short, consice and allows any number of sides and multiple tosses.

    BTW The // is defined-or :)


    Enjoy, Have FUN! H.Merijn
Re: Six-side dice roll calculator
by zentara (Archbishop) on Jul 20, 2007 at 11:39 UTC
Re: Six-side dice roll calculator
by goibhniu (Hermit) on Aug 22, 2007 at 18:08 UTC

    Late comment here; adding to the pile-on.

    I put "my first dice module" in this post. Part of what I like about it is the separation of presentation from logic (ok, not GREAT separation, but the programmer can choose between ->toString and ->toTextArt). It was when I was a young naive programmer (weeks ago!), so it's not great code, but feel free to adapt as you like.


    I humbly seek wisdom.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://627592]
Approved by grep
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2021-07-25 03:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?