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:
rspier
Date:
March 5, 2001 11:33
Subject:
Re: [ID 20010305.005] "use integer" doesn't make rand() return integers
Message ID:
15011.59838.290107.292862@rls.cx

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



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