March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -6,7 +6,8 @@ end
def rand_until_rep(n)
rands = {}
loop do r = rand(1..n)
loop do
r = rand(1..n)
return rands.size if rands[r]
rands[r] = true
end
@ -14,10 +15,11 @@ end
runs = 1_000_000
puts " n\tavg\texp.\tdiff\n_____________________________"
puts " N average exp. diff ",
"=== ======== ======== ==========="
(1..20).each do |n|
sum_of_runs = runs.times.inject(0){|sum, _| sum += rand_until_rep(n)}
avg = sum_of_runs / runs.to_f
analytical = (1..n).inject(0){|sum, i| sum += (n.factorial / (n**i).to_f / (n-i).factorial)}
puts "%2d %8.4f %8.4f %8.4f" % [n, avg, analytical, (avg/analytical - 1)*100]
puts "%3d %8.4f %8.4f (%8.4f%%)" % [n, avg, analytical, (avg/analytical - 1)*100]
end