added files

This commit is contained in:
2025-01-18 10:36:27 -05:00
parent a1398cfd45
commit cd4a93da3c
7 changed files with 195 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
import random
def monteCarlo(numPoints: int) -> float:
pInside = 0
for _ in range(numPoints):
x = random.uniform(-1, 1)
y = random.uniform(-1, 1)
# (x^2 + y^2 <= 1)
if x**2 + y**2 <= 1:
pInside += 1
# approx π
estimate = 4 * (pInside / numPoints)
return estimate
# I don't know if an example is needed, but I added one anyways
numPoints = 1000000 # higher -> more accurate
piApprox = monteCarlo(numPoints)
print(f"Approximated value of π: {piApprox}")