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 5, 2001 11:56
Subject:
Re: [ID 20010305.005] "use integer" doesn't make rand() return integers
Message ID:
3AA3EF1F.62BFE717@rowman.com
Robert Spier wrote:
> 
> f> -----------------------------------------------------------------
> f> The following program doesn't print a sequence of integers as one
> f> would expect.
> 
> f> use integer; for(;;) { print rand 10; }
> f> -----------------------------------------------------------------
> 
> I don't think this is a bug.
> 
> perldoc -f rand says:
> Returns a random fractional number greater than or equal t....
> 
> perldoc integer says:
>        integer - Perl pragma to compute arithmetic in integer
>        instead of double
> 
> As "rand" isn't really a perl-level integer computation, (it can
> almost be thought of as a black box that spits out "random fractional
> numbers"), I think the proper thing is to just convert it by hand if
> that's what you want:
> 
>     print int(rand 10);
> 
> (The patch to make rand return an integer under use integers is
> somewhat trivial..
> 
>   if (PL_hints & HINT_INTEGER)
>     value = floor(value);
> 
>   before the push..
> 
> I think.)
> 
> -R

I would like one of the mathematicians on the list to confirm that
using floor like that will in fact yield a random list; I suspect
that it won't.  In fact, that patch will return a list of zero's if
the default rand() is called while "use integer" is in effect.

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