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 6, 2001 14:16
Subject:
Re: [ID 20010305.005] "use integer" doesn't make rand() return integers
Message ID:
15013.24973.424830.56138@rls.cx

I'm still not sure if this is a good thing or not.

But, I made a patch anyway.  

See below.

I'm confused though.

The following code doesn't work as expected.
{
        use integer;
        my $x = 3.5 * 3;
        my $y = rand 10;
        print "$x $y\n";
}
(with my patched perl)

it does not output 9 and then an integer, it outputs, 9 <some float>.

(f.pl contains the above code.)
[rspier@speed perl-current]$ ./perl -Ilib -Minteger /tmp/f.pl 
9 9
[rspier@speed perl-current]$ ./perl -Ilib  /tmp/f.pl          
9 8.84369324500771

Is PL_hints not the right way to check for lexically scoped flags?

-R

>>>>> "TF" == Tony Finch <dot@dotat.at> writes:

TF> John Peacock <jpeacock@rowman.com> wrote:
>> abigail@foad.org wrote:
>>>  Having said that, having 'rand' returning integers when 'use
>>> integer' is in effect fits exactly in Perls DWIM nature.

TF> This was why I submitted the bug report.

>> I agree that it does fit DWIM, but since rand() returns a float
>> between 0 and 0.999999999, what would the corresponding integer
>> range be: 0-10, 0-100, 0-1000???  I think this is one of those
>> cases where the user has to make that determination for themselves.

TF> I suggest 0..1.

TF> Tony.  -- f.a.n.finch fanf@covalent.net dot@dotat.at "I never
TF> wanted to be a weather forecaster -- I wanted to be... a
TF> lumberjack!  Leaping from tree to tree as they float down the
TF> mighty rivers of British Columbia! The giant redwood! The larch!
TF> The The mighty scots pine! ..."


--- pp.c.orig   Mon Mar  5 15:45:09 2001
+++ pp.c        Tue Mar  6 16:37:35 2001
@@ -2398,11 +2398,17 @@
        value = POPn;
     if (value == 0.0)
        value = 1.0;
+    if (PL_hints & HINT_INTEGER) {
+      if (value == 1.0) 
+       value = 2.0; /* return [0,1] under use integer */
+    }
     if (!PL_srand_called) {
        (void)seedDrand01((Rand_seed_t)seed());
        PL_srand_called = TRUE;
     }
     value *= Drand01();
+    if (PL_hints & HINT_INTEGER)
+      value = floor( value );
     XPUSHn(value);
     RETURN;
 }

--- perlfunc.pod~       Wed Feb 28 11:25:51 2001
+++ perlfunc.pod        Tue Mar  6 15:54:00 2001
@@ -3494,6 +3494,10 @@
 large or too small, then your version of Perl was probably compiled
 with the wrong number of RANDBITS.)
 
+Under use L<integer>, rand will return a random integer greater than
+or equal to C<0> and less than the value of EXPR.  If EXPR is
omitted,
+rand will return either C<0> or C<1>.
+
 =item read FILEHANDLE,SCALAR,LENGTH,OFFSET
 
 =item read FILEHANDLE,SCALAR,LENGTH

--- /dev/null   Tue May  5 16:32:27 1998
+++ rand-integer.t      Tue Mar  6 16:35:22 2001
@@ -0,0 +1,47 @@
+#!./perl
+
+# this file contains the tests for rand() under q[use integer];
+
+
+BEGIN {
+  chdir "t" if -d "t";
+  @INC = '../lib';
+}
+
+use strict;
+use Config;
+
+print "1..3\n";
+
+use integer;
+
+my $err = 0;
+
+# test 1 - does rand under use integer only return ints
+for (10..100) {
+  my $r = rand($_);
+  $err++ if ( int($r) != $r );
+}
+print "not " if $err;
+print "ok 1\n";
+
+# test 2 - does rand() return only 1 or 0;
+# test 3 - does rand() return 1 sometimes.
+$err = 0;
+my $count = 0;
+for (10..100) {
+  my $r = rand;
+  $err++ if ($r != 0 && $r != 1);
+  $count++ if ($r == 1);
+}
+if ($err) {
+  print "not ok 2 # fractional numbers being returned\n"; 
+} else {
+  print "ok 2\n";
+}
+
+if (!$count) {
+  print "not ok 3 # no ones returned - bad RNG or rounding\n";
+} else {
+  print "ok 3\n";
+}



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