Skip to content

Instantly share code, notes, and snippets.

@chokkoyamada
Created November 10, 2013 16:08
Show Gist options
  • Save chokkoyamada/7400046 to your computer and use it in GitHub Desktop.
Save chokkoyamada/7400046 to your computer and use it in GitHub Desktop.
モンテカルロ法で円周率を求めるRubyスクリプト
ans0 = 0
sampling_num = 1000000
(1..sampling_num).each do
list = [Random.rand(1.0), Random.rand(1.0)]
range = Math.sqrt(list[0]**2 + list[1]**2)
if range < 1.0
ans0 += 1
end
end
p 4 * (ans0.to_f / sampling_num)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment