Statistics/Basic: Difference between revisions
m
→{{header|Sidef}}: updated code and output
(added Easylang) |
m (→{{header|Sidef}}: updated code and output) |
||
Line 5,233:
{{trans|Ruby}}
<syntaxhighlight lang="ruby">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 =
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) }</syntaxhighlight>
{{out}}
<pre style="height: 40ex; overflow: scroll">
size: 100
mean: 0.539719181395696620109634051345884432579835159541
stddev: 0.283883840711089795862044996985935095942987013707
0.0:
0.1:
0.2:
0.3:======================================================================
0.4:==========================================================
0.5:======================================================================
0.6:
0.7:======================================================================
0.8:======================================================================
0.9:================================================================
size: 1000
mean: 0.509607463325018405029035982604757578351179500375
stddev: 0.291051486526422985516729469185300756396357843712
0.0:==========================================================
0.1:========================================================
0.2:================================================================
0.3:========================================================
0.4:
0.5:=====================================================================
0.6:
0.7:===========================================================
0.8:==========================================================
0.9:======================================================================
size: 10000
mean: 0.501370967820671948202377775772729161752514666335
stddev: 0.288601021921015908441703525737039264149088197141
0.0:
0.1:====================================================================
0.2:
0.3:=================================================================
0.4:======================================================================
0.5:=================================================================
0.6:===============================================================
0.7:===================================================================
0.8:==================================================================
0.9:====================================================================
</pre>
|