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:
John Peacock
Date:
March 7, 2001 18:57
Subject:
Re: [ID 20010305.005] "use integer" doesn't make rand() return integers
Message ID:
3AA6F53B.E01B6EF6@rowman.com
"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
other
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. 
I'll
try and scan a CPAN CD that I have and see what incidence I can find. 
I'll
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,
like
+, -, /, *, <, etc.  If the user (who oddly enough knows what effect
they
want), wants to have an integer value in a specific range, let them use

	int(rand($limit))

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
> bwarnock@capita.com

John Peacock

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