RosettaCodeData/Task/Monte-Carlo-methods/Ruby/monte-carlo-methods.rb

9 lines
239 B
Ruby
Raw Permalink Normal View History

2013-04-10 21:29:02 -07:00
def approx_pi(throws)
times_inside = throws.times.count {Math.hypot(rand, rand) <= 1.0}
4.0 * times_inside / throws
end
[1000, 10_000, 100_000, 1_000_000, 10_000_000].each do |n|
2018-06-22 20:57:24 +00:00
puts "%8d samples: PI = %s" % [n, approx_pi(n)]
2013-04-10 21:29:02 -07:00
end