Created
August 22, 2021 08:57
-
-
Save WangYihang/262e48b30c7699dc5485abe05c6ecef5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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