Skip to content

Instantly share code, notes, and snippets.

@YuukiToriyama
Created December 29, 2019 14:16
Show Gist options
  • Save YuukiToriyama/5164e3441a190cc0cda9833224b82218 to your computer and use it in GitHub Desktop.
Save YuukiToriyama/5164e3441a190cc0cda9833224b82218 to your computer and use it in GitHub Desktop.
モンテカルロ法
#!/usr/bin/env lua
function calcPi(m)
local count = 0
for i = 1, m do
x, y = math.random(), math.random()
if x^2 + y^2 < 1 then
count = count + 1
end
end
-- (円の面積):(正方形の面積) = r^2pi:4r^2
-- (円の面積):(正方形の面積) = (円の中に入った点の数):(打った点の総数)
return 4 * count / m
end
print(calcPi(10000000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment