in reply to Math all gone wrong...

Any bets he's using ActivePerl? Here's the output I get on under ActivePerl 633 (5.6.1.633) on Win2k:

Error rate = 1 every 10, errors = 100246 100029 99946 99592 100069 Error rate = 1 every 100, errors = 9935 10090 10073 9879 10043 Error rate = 1 every 1000, errors = 998 973 966 967 1003 Error rate = 1 every 10000, errors = 96 96 75 93 97 Error rate = 1 every 100000, errors = 0 0 0 0 0



<-> In general, we find that those who disparage a given operating system, language, or philosophy have never had to use it in practice. <->

Replies are listed 'Best First'.
Re: Re: Math all gone wrong...
by carlos fandango (Novice) on Dec 01, 2002 at 12:04 UTC
    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?
      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