my $ssum = 0; # $sum not needed here for my $i (1..1000){ my $sum = 0; # but here. for my $j (1..$sampleSize){ $sum+=rand(); } $sum/=$sampleSize; $ssum+=$sum; }