Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I decided to generalize in a different direction, show how you could use a few different programming ideas with this one. Feel free to play around with different sizes, numbers, and choices of die to throw. People who want to play should consider things like using GD::Graph to start producing bar charts etc.
#! /usr/bin/perl -w use strict; my @dist = dice_distribution( sides => 6, number => 4, slice => [1, 2, 3], ); my $average = sum( map $_*$dist[$_], 0..$#dist ) / sum(@dist); print "The average is $average\n"; sub dice_distribution { my %args = @_; my $sides = $args{sides} || 6; my $number = $args{number} || 3; my $slice = $args{slice} || [0..($number - 1)]; my @distribution; nested_for( sub { my @dice = (sort {$a <=> $b} @_)[@$slice]; $distribution[ sum(@dice) ]++; }, map [1..$sides], 1..$number, ); # Quiet warnings $_ ||= 0 for @distribution; return @distribution; } sub sum { my $sum = 0; $sum += $_ for @_; return $sum; } sub nested_for { bind_loops(@_)->(); } sub bind_loops { my $fn = shift; my $range = shift; my $sub = sub {$fn->($_, @_) for @$range}; return @_ ? bind_loops($sub, @_) : $sub; }
Admittedly I didn't make it shorter. But then again, as pointed out in The path to mastery, I don't think that shorter should always be the immediate aim of someone who is trying to learn...

(Yes, a key part of this bears a suspicious resemblance to Re (tilly) 1 (perl): What Happened...(perils of porting from c). If you are puzzled over how this works, figuring out that first may be a good idea.)


In reply to Re: Rolling DND Dice. by tilly
in thread Rolling DND Dice. by grendelkhan

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-19 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found