Skip to content

Instantly share code, notes, and snippets.

@WangYihang
Created August 22, 2021 08:57
Show Gist options
  • Save WangYihang/262e48b30c7699dc5485abe05c6ecef5 to your computer and use it in GitHub Desktop.
Save WangYihang/262e48b30c7699dc5485abe05c6ecef5 to your computer and use it in GitHub Desktop.
import random
def solve():
'''
area of unit circle at (0.5, 0.5) = pi * (0.5 ^ 2)
area of unit trangle at (0.5, 0.5) = 1 * 1
the ratio of the number of points that fall in the circle to the number
of all points will approximate the ratio of the area of the circle to
the area of the entire square.
'''
N = 1000000
i = 0
for _ in range(N):
x, y = random.random(), random.random()
if (x - 0.5) ** 2 + (y - 0.5) ** 2 <= 0.25: i += 1
return 4 * i / N
print(solve())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment