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
John Peacock
March 7, 2001 18:57
Re: [ID 20010305.005] "use integer" doesn't make rand() return integers
Message ID:
"Bryan C. Warnock" wrote:
> > is sufficient.  There is no reason to change the meaning of 'use
> > integer'
> > to suit one person's DWIM.
> Again, one person is a broad assumption.

I'm not really assuming that only one person would make that assumption,
just that it is not something obviously lacking in Perl.  None of the
floating point mathematic functions change under 'use integer'.  I don't
think that there is enough reason to alter basic behavior.

> 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)).

Ah, but how many of those uses was under 'use integer' or could easily
be changed to be under 'use integer'?  I suspect that there are very few
programs which 'use integer' for more than a small part of their code. 
try and scan a CPAN CD that I have and see what incidence I can find. 
look for "rand(" as well.

> 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.
> Personally, I agree with John, in that rand shouldn't be overloaded by 'use
> integer'.  (The rationale behind wanting the integers in the first place
> differs, I think.)
> And, of course, Abigail doesn't want rand() overloaded, either - just
> rand($num).  But looking at the usage, the question that should be asked is
> why doesn't rand($num) just return an integer regardless of 'use integer'.?

Because the only time 'use integer' has any effect is math operations,
+, -, /, *, <, etc.  If the user (who oddly enough knows what effect
want), wants to have an integer value in a specific range, let them use


which will accomplish exactly what they want.

> A floating point value can still be obtained with rand()*$num.  (Unless
> 'use integer' *is* in effect.  Which makes it oh-so-confusing....)
> --
> Bryan C. Warnock

John Peacock

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