Created
October 1, 2020 05:26
-
-
Save pycrawling/d19b99908d508bb78310a0f32be69796 to your computer and use it in GitHub Desktop.
Estimating the value of Pi using Monte Carlo simulation
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
# Estimating the value of Pi using Monte Carlo simulation | |
# https://comdoc.tistory.com | |
from random import random | |
def is_in(r): | |
x = random() * r * 2 | |
y = random() * r * 2 | |
return (x - r) ** 2 + (y - r) ** 2 <= r ** 2 | |
total = 10000 | |
success = 0 | |
for _ in range(total): | |
if is_in(1): | |
success += 1 | |
print(success / total * 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment