Given a number, as in rand(6), it generates an integer which is at least 0 and less than the number it was given (0 to 5 in this case)

Almost, but not quite.

% perl -le 'print rand(6)' 4.45325434943245

This will take the number between 0 and less than 1 and multiply it by 6, nearly always leaving stuff behind the decimal point.

If you really don't want the fractional part you have to shave it off with either int($num) or sprintf('%.0f', $num). In the present case it'll work just fine, since Perl will Do The Right Thing and treat the number as an integer anyway, in order to use it as an index into the array. But were the OP to print the number itself, they may be surprised by the result.

• another intruder with the mooring in the heart of the Perl


In reply to Re^2: Creating a random generator by grinder
in thread Creating a random generator by Lady_Aleena

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.