develooper Front page | perl.perl5.porters | Postings from March 2001

Re: [ID 20010305.005] "use integer" doesn't make rand() return integers

Thread Previous
From:
John Peacock
Date:
March 5, 2001 10:12
Subject:
Re: [ID 20010305.005] "use integer" doesn't make rand() return integers
Message ID:
3AA3D530.E1F4124B@rowman.com
There is no suggestion in either the docs for integer or rand that would
seem to suggest that this should work.  If you want a random integer
between 0 and 10, try this instead:

for(;;) {
        print int(rand(10)), "\n";
}


NOTE:  I have no idea if this will be a truly random distribution or if

for(;;) {
        print int(rand()*10), "\n";
}

would be better.  Random number generation is easy to mess up when 
performing other operations on the sequence.

I recommend that this "bug" either be closed or turned into a Request
for Enhancement to the "use integer" pragma.

John Peacock

fanf@dotat.at wrote:
> 
> This is a bug report for perl from dot@dotat.at,
> generated with the help of perlbug 1.26 running under perl 5.00503.
> 
> -----------------------------------------------------------------
> The following program doesn't print a sequence of integers
> as one would expect.
> 
> use integer;
> for(;;) {
>         print rand 10;
> }
> -----------------------------------------------------------------
> 
> ---
> Site configuration information for perl 5.00503:

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About