Last active
July 27, 2020 02:12
-
-
Save redwrasse/8f890ec2df0cd11ca0b044d4bed2697e to your computer and use it in GitHub Desktop.
monte carlo approximation of pi
This file contains hidden or 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 compute_pi(): | |
sm = 0. | |
n = 10**7 | |
for i in range(n): | |
x1, x2 = 2 * (random.uniform(0, 1) - 0.5), 2 * (random.uniform(0, 1) - 0.5) | |
r = x1**2 + x2**2 | |
if r <= 1.0: | |
sm += 1. | |
return 4 * sm / n | |
print(compute_pi()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment