Skip to content

Instantly share code, notes, and snippets.

@sdamashek
Created May 26, 2017 14:41
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 sdamashek/baeea6c73d0f15a7b6620f28c63d7e97 to your computer and use it in GitHub Desktop.
Save sdamashek/baeea6c73d0f15a7b6620f28c63d7e97 to your computer and use it in GitHub Desktop.
import math
import random
import sys
def main():
num = int(sys.argv[1])
points_in = 0
for _ in range(num):
px, py = random.random() * 2 - 1, random.random() * 2 - 1
if (px**2 + py**2) <= 1:
points_in += 1
pi_approx = 4.0 * points_in / num
print('Approx = {}'.format(pi_approx))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment