Statistics/Basic
func generate_statistics(n) {
var(sum=0, sum2=0)
var hist = 10.of(0)
n.times {
var r = 1.rand
sum += r
sum2 += r**2
hist[10*r] += 1
}
var mean = sum/n
var stddev = sqrt(sum2/n - mean**2)
say "size: #{n}"
say "mean: #{mean}"
say "stddev: #{stddev}"
var max = hist.max
for i in ^hist {
printf("%.1f:%s\n", 0.1*i, "=" * 70*hist[i]/max)
}
print "\n"
}
[100, 1000, 10000].each {|n| generate_statistics(n) }