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
March 7, 2001 15:07
Re: [ID 20010305.005] "use integer" doesn't make rand() return integers
Message ID:
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


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About