wilstephens has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys Just a few questions ...

Is the perl rand() function zero inclusive? From my interpertation from perldoc rand I think it is.

And if so is the return data type a float? If so, how do you make it a whole number? I didn't see a floor() or round() function anywhere in perldoc perlfunc

Thanks

--
Wiliam Stephens <wil@stephens.org>

Replies are listed 'Best First'.
(crazyinsomniac) Re: perl rand() function
by crazyinsomniac (Prior) on Apr 16, 2002 at 09:59 UTC
    The pod for the rand function says
    rand Returns a random fractional number greater than or equal to "0"
    So yes, it is zero inclusive.

    The return data type is not float (there is no such thing in perl). See perldata and perlnumber for more information (and a better explanation of perl number semantics -- to you/me they're all scalars, but some functions/operators are picky).

    Back to perldoc, perlfunc says:

    Numeric functions "abs", "atan2", "cos", "exp", "hex", "int", "log", "oct", "rand", "sin", "sqrt", "srand"
    see int -- it just returns the integer portion of a number. It does not round. If you wish to round, the int documentation itself reccomends "sprintf", "printf", or the "POSIX::floor" and "POSIX::ceil". There is a tutorial on (s)printf in our Tutorials section. POSIX is a standard module, and ceil and floor do what you'd expect.

    Now on a different note, it's easy to miss int tucked in the perlfunc, so do like How to RTFM says (great read), use the help of a search engine or even grep/find, especially when you've got a great keyword like round.

    update: moodster below links to a question from the perlfaq which is invaluable (and which is also mentioned in How to RTFM -- good advice).

     
    ______crazyinsomniac_____________________________
    Of all the things I've lost, I miss my mind the most.
    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

      Thanks for the info and all the links. It completly jumped out of my mind to use sprtintf/printf but I hadn't of heard of the POSIX module.

      Thanks for the heads up!

      --
      Wiliam Stephens <wil@stephens.org>
Re: perl rand() function
by moodster (Hermit) on Apr 16, 2002 at 10:04 UTC
(MeowChow) Re: perl rand() function
by MeowChow (Vicar) on Apr 16, 2002 at 10:46 UTC
    Yes, it is zero inclusive, however, I wouldn't be waiting around for perl -e '1 while rand' to finish within my lifetime.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print

      I'm so sorry that MeowChow was diagnosed as terminally ill with less than 1 second expected to live! I'm just glad the submit button got pressed before the end.

      % perl -de 0 ... DB<1> $c=0; print ''.localtime(),$/; $c++ while rand; print ''.local +time(),$",$c,$/ Tue Apr 16 07:57:24 2002 Tue Apr 16 07:57:24 2002 55285 DB<2> $c=0; print ''.localtime(),$/; $c++ while rand; print ''.local +time(),$",$c,$/ Tue Apr 16 07:57:28 2002 Tue Apr 16 07:57:28 2002 82230
      ;)

      Update: To be more informative rather than just humorous, I'll note that     perl -MConfig -e 'print $Config{randbits}' reports 15 for me. If I managed to build Perl such that this value got all the way up to 32, then I'd project MeowChow's life expectancy at a bit under 45 minutes. If I got that up to 48, then we'd be talking closer to 5 years. And if I got it up to 64, then I'd agree with MeowChow that waiting around 350,000 years wasn't worthwhile. (:

              - tye (no, I don't own a Cray)
        tye, you absolutely slay me :)
           MeowChow                                   
                       s aamecha.s a..a\u$&owag.print
Re: perl rand() function
by func (Acolyte) on Apr 16, 2002 at 17:53 UTC

    I was playing around with rand() last night and wanted rand() to select a random number from a range. After much brain racking and some help from learning perl, I came up with this:

    #!/usr/bin/perl -w use strict; srand; # produce n random numbers my $lowest_number=1; my $highest_number=49; my @number_range=($lowest_number...$highest_number); my @lottery_numbers=(1...6); my $a = 1; do { # A random element from @number_range is selected and # placed in to @X # $lottery_numbers[$a] = $number_range[rand $highest_number]; $a ++; } until $a > 6; print "@lottery_numbers[1...6]\n";

    While this may not be of use to the original poster, I hope it will be useful to some monk in the future. :)

    Yet another perl newbie.

      func,

      This isn't gonna work. If I want random from 50 - 100, you code puts 50 to 100 in an array then chooses random indexes from 0 - 99. But there are only 51 items in the array. To fix this change your rand parameter from $highest_number to @number_range.

      Here's another way:

      YuckFoo

      #!/usr/bin/perl use strict; my ($beg, $end) = qw(10 20); my $range = ($end - $beg) + 1; for (1..6) { print int(rand($range) + $beg) . "\n"; }

        Thanks. I never knew rand() could have an array as an expression. Also, I'll have to do more testing before I post my code. :)

      my @number_range=($lowest_number...$highest_number);

      a side note: what you've got there is terribly inefficient. You are constructing an array of size ($highest_number - $lowest_number) which is going to be pretty painful if your range is 0 to 100,000,000.