RosettaCodeData/Task/Statistics-Basic/Phix/statistics-basic.phix

35 lines
878 B
Plaintext

function generate_statistics(integer n)
sequence hist = repeat(0,10)
atom sum_r = 0,
sum_squares = 0.0
for i=1 to n do
atom r = rnd()
sum_r += r
sum_squares += r*r
hist[floor(10*r)+1] += 1
end for
atom mean = sum_r / n
atom stddev = sqrt((sum_squares / n) - mean*mean)
return {n, mean, stddev, hist}
end function
procedure display_statistics(sequence x)
atom n, mean, stddev
sequence hist
{n, mean, stddev, hist} = x
printf(1,"-- Stats for sample size %d\n",{n})
printf(1,"mean: %g\n",{mean})
printf(1,"sdev: %g\n",{stddev})
for i=1 to length(hist) do
integer cnt = hist[i]
string bars = repeat('=',floor(cnt*300/n))
printf(1,"%.1f: %s %d\n",{i/10,bars,cnt})
end for
end procedure
for n=2 to 5 do
display_statistics(generate_statistics(power(10,n+(n=5))))
end for