Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
20
Task/Statistics-Basic/Ruby/statistics-basic.rb
Normal file
20
Task/Statistics-Basic/Ruby/statistics-basic.rb
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
def generate_statistics(n)
|
||||
sum = sum2 = 0.0
|
||||
hist = Array.new(10, 0)
|
||||
n.times do
|
||||
r = rand
|
||||
sum += r
|
||||
sum2 += r**2
|
||||
hist[(10*r).to_i] += 1
|
||||
end
|
||||
mean = sum / n
|
||||
stddev = Math::sqrt((sum2 / n) - mean**2)
|
||||
|
||||
puts "size: #{n}"
|
||||
puts "mean: #{mean}"
|
||||
puts "stddev: #{stddev}"
|
||||
hist.each_with_index {|x,i| puts "%.1f:%s" % [0.1*i, "=" * (70*x/hist.max)]}
|
||||
puts
|
||||
end
|
||||
|
||||
[100, 1000, 10000].each {|n| generate_statistics n}
|
||||
Loading…
Add table
Add a link
Reference in a new issue