Skip to content

Instantly share code, notes, and snippets.

@redwrasse
Last active July 27, 2020 02:12
Show Gist options
  • Save redwrasse/8f890ec2df0cd11ca0b044d4bed2697e to your computer and use it in GitHub Desktop.
Save redwrasse/8f890ec2df0cd11ca0b044d4bed2697e to your computer and use it in GitHub Desktop.
monte carlo approximation of pi
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