in reply to Perl rand() generates larger numbers for small sample size, bug!
You aren't clearing $sum at the beginning of each outer loop. Try this:
#!/usr/bin/perl use strict; use warnings; my $sampleSize = 7; my ($sum, $ssum); for my $i (1..1000){ $sum = 0; for my $j (1..$sampleSize){ $sum+=rand(); } $sum/=$sampleSize; $ssum+=$sum; } $ssum/=1000; print "$ssum\n";
If you think you've found a show-stopping bug in a core function that others have been using successfully for years or decades, it's a good idea to examine your own code before declaring it a bug in Perl.
Edited to add: Besides, how could the rand() function "know" how many times you're about to call it, or how many you're not going to call it? Short of a maliciously buggy dev-teasing version of Perl, which is too far-fetched to consider, there's no possible mechanism that could do this.
|
---|