in reply to Re: Math all gone wrong...
in thread Math all gone wrong...

Hmm - thanks all - you've hit the nail on the head. I'm running this on a Win98 machine using ActivePerl. The prizes go to all those who bet that way. Clearly ActivePerl has a screwy rand function, since the code works fine on the others (many many thanks to those above who tried it on another system and showed me that I'm not going insane). Is there a way to fix this in ActivePerl, or am I doomed to reduced probabilities?

Replies are listed 'Best First'.
Re: Re: Re: Math all gone wrong...
by carlos fandango (Novice) on Dec 01, 2002 at 15:34 UTC
    Now fixed (see modified code and new output below) on ActivePerl, thanks to the previous replies. Has this been a common problem in ActivePerl? I'm pretty new to this, and naively thought the rand function would be platform independent.
    $seed = rand(100); for ($x=10; $x<10000000; $x=$x*10){ $error_rate = $x; print "Error rate = 1 every $x, errors = "; for ($y=0;$y<5;$y++){ errortest(); } print "\n"; } sub errortest { $errors = 0; for ($n=0;$n<1000000;$n++){ $random = int (better_random($error_rate)); if ($random == 1){$errors++} } print $errors, " "; } sub better_random { $seed = ($seed * 65539) % 2 ** 31; $this_random = $_[0] * ($seed / 2 ** 31); return $this_random; } Ouput: Error rate = 1 every 10, errors = 99259 100049 99986 99998 99818 Error rate = 1 every 100, errors = 10166 10098 10056 10107 10032 Error rate = 1 every 1000, errors = 967 1013 1009 948 1004 Error rate = 1 every 10000, errors = 96 108 112 116 104 Error rate = 1 every 100000, errors = 16 11 7 8 10 Error rate = 1 every 1000000, errors = 0 3 1 1 0