The next steps are to abstract the process of rolling a 6-sided die, and then to abstract the process of creating an n-sided die to roll. The latter step is a bit advanced, but just be aware it's possible. Also be aware that the built-in rand function may not be all that random.

c:\@Work\Perl>perl -wMstrict -le "printf qq{%d }, die6() for 1 .. 10; print ''; ;; sub die6 { return 1 + int rand 6; } ;; ;; sub make_die { my ($sides) = @_; ;; return sub { return 1 + int rand $sides; }; } ;; my $die_20 = make_die(20); my $die_5 = make_die(5); ;; printf qq{20: %d; 5: %d \n}, $die_20->(), $die_5->() for 1 .. 6; " 1 5 4 4 6 5 4 1 1 1 20: 9; 5: 2 20: 13; 5: 1 20: 6; 5: 1 20: 9; 5: 3 20: 9; 5: 1 20: 5; 5: 1

In reply to Re: New to Perl: Hashes and int(rand()) by AnomalousMonk
in thread New to Perl: Hashes and int(rand()) by Einzig

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.