forked from DevopediaOrg/python-for-kids
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_pi.py
More file actions
33 lines (23 loc) · 635 Bytes
/
find_pi.py
File metadata and controls
33 lines (23 loc) · 635 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
import random
import math
def in_circle(x, y):
return x**2 + y**2 < 1
def get_pi(inside, total):
return (inside / total) * 4
# Initialize
random.seed(12345)
total = 10**7
inside_count = 0
# Generate random points within the square
for i in range(total):
# x and y are in range [-1, 1]
x = 2 * random.random() - 1
y = 2 * random.random() - 1
if in_circle(x, y):
inside_count += 1
# Print progress
if i % (total//10) == 0:
print(get_pi(inside_count, i+1))
# Print final value
print("Calculated value of Pi:", get_pi(inside_count, total))
print(" Actual value of Pi:", math.pi)