Talk:Statistics/Basic: Difference between revisions

→‎Perl style: new section
(→‎Random number spec: ". It doesn't matter if you chose to use open or closed range.")
(→‎Perl style: new section)
Line 42:
 
:Hi Donal, the task goes on to state: ''". It doesn't matter if you chose to use open or closed range."'' --[[User:Paddy3118|Paddy3118]] 12:32, 19 October 2011 (UTC)
 
== Perl style ==
 
I was looking at the perl section, and the code there doesn't seem representative of modern perl style. In particular, the c-style for loop syntax is not very perl-ish. I'd like to change the syntax up a little bit:
 
<lang perl>my $hist_rows = 10;
my @histogram = map { 0 } 1..$hist_rows; # I filled the array to ensure that $#histogram == $hist_rows-1 - this may be unnecesssary
# alternatively, do away with the $hist_rows variable altogether, using map {0} 1..10 and referring to @histogram instead of $hist_rows below
my $sum = 0;
my $sum_squares = 0;
my $n = $ARGV[ 0 ];
 
for (1..$n) { # no need to name a variable that is never used
my $current = rand( 1 );
$sum += $current;
$sum_squares += $current ** 2;
$histogram[ $current * $hist_rows ] += 1;
}
 
my $mean = $sum / $n;
 
print "$n numbers\n", # concatenation replaced with multiple arguments to print
"Mean: $mean\n",
"Stddev: ", sqrt(($sum_squares / $n) - ($mean ** 2)), "\n";
 
for my $row_counter (0..$#histogram) {
printf( "%.1f - %.1f : ", $row_counter/$hist_rows, (1/$hist_rows) + ($row_counter/$hist_rows));
 
print "*" x ($histogram[ $row_counter ]*(30/($n/10))); # loop replaced with x operator
print "\n";
}</lang>
(The comments above are notes to the talk page, and I would erase them before posting the above to the article.)
 
Needless to say, TMTOWTDI, but this seems more like idiomatic perl to me, and, at least to me, it's more readable.
Anonymous user