-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonteCarlo.py
More file actions
36 lines (30 loc) · 960 Bytes
/
monteCarlo.py
File metadata and controls
36 lines (30 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Obliczanie przybliżonej wartości PI metodą Monte Carlo
import random
import time
r = input("Podaj promień koła: ")
pointsNum = input("Podaj liczbę punktów do wylosowania: ")
try:
r = float(r)
pointsNum = int(pointsNum)
points = []
countOfPointsIn = 0
countOfPointsOut = 0
for i in range(pointsNum):
x = random.random() * 2 * r
y = random.random() * 2 * r
points.append({'x': x, 'y': y})
start = time.time()
for i in range(pointsNum):
if((points[i]['x'] - r) ** 2 + (points[i]['y'] - r) ** 2 <= r ** 2):
# points[i]['inCircle'] = True
countOfPointsIn += 1
else:
countOfPointsOut += 1
pi = countOfPointsIn/countOfPointsOut
end = time.time()
print(f"Punkty wewnątrz koła: {countOfPointsIn}\n"
+ f"Punkty na zewnątrz koła: {countOfPointsOut}\n"
+ f"Przybliżenie PI: {pi}\n"
+ f"Trwało to {round((end - start) * 100)/100} s")
except(ValueError):
quit()