develooper Front page | perl.perl5.porters | Postings from March 2001

Re: [ID 20010305.005] "use integer" doesn't make rand() return integers

Thread Previous | Thread Next
From:
abigail
Date:
March 7, 2001 15:07
Subject:
Re: [ID 20010305.005] "use integer" doesn't make rand() return integers
Message ID:
20010307230835.20487.qmail@foad.org
On Wed, Mar 07, 2001 at 05:58:01PM -0500, Bryan C. Warnock wrote:
> 
> A quick (and albeit inconclusive) scan of every piece of perl I could get a 
> hold of (without undoing my CPAN mirror), about half the uses of 'rand' are 
> exactly as you have above - int(rand($num)).
> 
> But, and this is a strong but, nearly every use of rand($num) was used as 
> int(rand($num)) - in other words, for its integer value.  
> 
> The other half of rand uses were as 'rand()' - and that, too, was almost 
> exclusively used for its floating point value.

Yes, but how useful is the floating point value of rand() in a "use integer"
context? As soon as you use it an arithmetic operation, including comparison,
it gets rounded.

The following will never terminate for instance:

    {   use integer;
        print ++ $i, "\n";
        redo unless rand () > 0.1
    }


Abigail

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About