Skip to content

Instantly share code, notes, and snippets.

@jesusalber1
Created February 26, 2019 22:00
Show Gist options
  • Save jesusalber1/8f2324b9fb01d0a78532ea9bf424575e to your computer and use it in GitHub Desktop.
Save jesusalber1/8f2324b9fb01d0a78532ea9bf424575e to your computer and use it in GitHub Desktop.
Estimating Pi using the Monte Carlo method
import random
import math
inner = 0
total = 0
for i in range(10000):
x = random.random()
y = random.random()
if (math.sqrt(x**2 + y**2) < 1):
inner += 1
total +=1
estimated_pi = 4 * (inner / total)
print('Estimated PI = {}'.format(estimated_pi))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment