Anonymous user
Verify distribution uniformity/Naive: Difference between revisions
Verify distribution uniformity/Naive (view source)
Revision as of 14:11, 10 August 2009
, 14 years ago→{{header|Ruby}}: yield to a block instead of a potentially clumsy proc argument
(add Ruby) |
(→{{header|Ruby}}: yield to a block instead of a potentially clumsy proc argument) |
||
Line 48:
=={{header|Ruby}}==
{{trans|Tcl}}
<lang ruby>def distcheck(
unless block_given?
raise ArgumentError, "pass a block to this method"
end
h = Hash.new(0)
n.times {h[
target = 1.0 * n / h.length
h.each do |key, value|
if (value - target).abs > 0.01 * delta * n
raise StandardError,
Line 58 ⟶ 63:
end
end
h.keys.sort.each {|k| print "#{k} #{h[k]} "}
puts
Line 63 ⟶ 69:
if __FILE__ == $0
begin
distcheck( lambda {rand(10)}, 100_000)▼
distcheck(
rescue StandardError => e
p e
end
end</lang>
output:
<pre>0 9986 1 9826 2 9861 3 10034 4 9876 5 10114 6 10329 7 9924 8 10123 9 9927
▲distcheck.rb:7:in `distcheck': distribution potentially skewed for 'false': expected around 50000.0, got 94912 (StandardError)</pre>
=={{header|Tcl}}==
|