mirror of
https://github.com/ION606/COGMOD-HWI.git
synced 2026-05-14 22:16:57 +00:00
added files
This commit is contained in:
@@ -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}")
|
||||
Reference in New Issue
Block a user