rand returns a random1, uniformly distributed number. To quote the documents, "Returns a random fractional number greater than or equal to 0 and less than the value of EXPR. (EXPR should be positive.) If EXPR is omitted, the value 1 is used. Currently EXPR with the value 0 is also special-cased as 1 - this has not been documented before perl 5.8.0 and is subject to change in future versions of perl."

So, if you pass 0 to rand(), it currently returns a value, x|0 ≤ x < 1. I believe it does the same think if you pass undef to rand, but RTFM2. How you handle your value of number (incidentally,

$my number = 10;
is not valid; I'm guessing you meant to write:
my $number = 10;
but I'm sure you noticed the error message from perl!) depends on what you want to do. Possibly, something like
$value = $user_input or 10; $x = rand($value);
may suffice, but I think that it most likely won't.

Now, if you want to guarantee that rand always returns the same sequence of values, you've got to explicitly seed it, using srand. Once.


1Pedantically, they're likely not truly random; they're almost certainly pseudo-random.

2The manual is definitely a worthwhile read, too. As an old connoisseur of computing documents, Perl's is definitely deserving of accolades.


emc

Information about American English usage here and here.

Any Northeastern US area jobs? I'm currently unemployed.

</div

In reply to Re: Re-assign value to subroutine by swampyankee
in thread Re-assign value to subroutine by props

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.