I wanted to test if the Perl's rand() function is fair or biased. I performed couple of different tests using different sample sizes. First I generated 7 random numbers between 0 and 1 and averaged them. This average should be close to 0.5. I repeated this test thousand times and calculated average of 1000 averages to see if it is any different from 0.5. Surprisingly it was 0.58. Following code describes this experiment. No matter how many times I run the following code the value never reaches close to 0.5.
$sampleSize = 7; for $i (1..1000){ for $j (1..$sampleSize){ $sum+=rand(); } $sum/=$sampleSize; $ssum+=$sum; } $ssum/=1000; print "$ssum\n";
I performed the same experiment but now with $sampleSize=10 or $sampleSize=100. I received 0.55 and 0.50 as output for 10 and 100 respectively. It seems that with smaller sample size, rand() functions is biased to produce larger numbers but not the smaller number.
Since I am not expert in statistics, to test if this is the phenomena of statistics, I performed the same experiment in R with small sample size (7) using following code. However, it generated value very close to 0.5. Please refer to the following R code.
Irrespective of the sample size (7,10 or 100), R produced same value but Perl's rand() function failed. I also tested rand() and irand() functions of Math::Random::Secure module of perl. They also tend to produce larger values for small sample size.for (i in 1:1000 ) { x2[i]=mean(runif(7, 0, 1)) } mean(x2)
Please use the above code to reproduce these results. Any help or suggestions would be highly appreciated.
Never mind guys. I made a mistake in my code. A small silly mistake. Just needed to empty my $sum variable after each of 1000 iterations. Thank you for your time.
Best,
Ravi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |