Skip to content

Instantly share code, notes, and snippets.

@syphh
Created June 27, 2022 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syphh/785b28c01b7757bf7a2541f54390dc7e to your computer and use it in GitHub Desktop.
Save syphh/785b28c01b7757bf7a2541f54390dc7e to your computer and use it in GitHub Desktop.
import numpy as np
def approximate_pi(n):
points = np.random.uniform(-1, 1, (n, 2))
inside = np.sum(points[:,0]**2+points[:,1]**2 <= 1)
k = inside/n
return 4*k
n = 1000000
approximation = approximate_pi(n)
print('Approximation:', approximation)
print('Error:', abs(approximation-np.pi))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment