-
Notifications
You must be signed in to change notification settings - Fork 1
/
pi.py
36 lines (26 loc) · 922 Bytes
/
pi.py
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
import random
print(f"Random is at: {random.__file__}")
INTERVAL = 1000
circle_points = 0
square_points = 0
# Total Random numbers generated= possible x
# values* possible y values
for i in range(INTERVAL**2):
# Randomly generated x and y values from a
# uniform distribution
# Range of x and y values is -1 to 1
rand_x = random.uniform(-1, 1)
rand_y = random.uniform(-1, 1)
# Distance between (x, y) from the origin
origin_dist = rand_x**2 + rand_y**2
# Checking if (x, y) lies inside the circle
if origin_dist <= 1:
circle_points += 1
square_points += 1
# Estimating value of pi,
# pi= 4*(no. of points generated inside the
# circle)/ (no. of points generated inside the square)
pi = 4 * circle_points / square_points
## print(rand_x, rand_y, circle_points, square_points, "-", pi)
# print("\n")
print("Final Estimation of Pi =", pi)