Skip to content

Instantly share code, notes, and snippets.

@pycrawling
Created October 1, 2020 05:26
Show Gist options
  • Save pycrawling/d19b99908d508bb78310a0f32be69796 to your computer and use it in GitHub Desktop.
Save pycrawling/d19b99908d508bb78310a0f32be69796 to your computer and use it in GitHub Desktop.
Estimating the value of Pi using Monte Carlo simulation
# 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