Created
February 26, 2019 22:00
-
-
Save jesusalber1/8f2324b9fb01d0a78532ea9bf424575e to your computer and use it in GitHub Desktop.
Estimating Pi using the Monte Carlo method
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 | |
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