-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi.py
38 lines (26 loc) · 961 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
37
38
# This program will use the Monte Carlo vers of estimating Pi
# Author: Yiping (Allison)
# December 2017
import random
def generate_x_y_val():
random_val = random.uniform(-1, 1)
return random_val
def probability_in_circle(num_iterations):
#count loop for probability in circle
num_in_circle = 0
for _ in range(num_iterations):
x_val = generate_x_y_val()
y_val = generate_x_y_val()
if x_val**2 + y_val**2 == 1:
num_in_circle += 1
elif x_val**2 + y_val**2 < 1:
num_in_circle += 1
return num_in_circle / num_iterations
def main():
seed = int(input('Enter the seed for the random number generator: '))
random.seed(seed)
num_iterations = int(input('Enter the number of iterations to run: '))
pi = probability_in_circle(num_iterations) * 4
print('The value of pi is %.3f.' % pi)
if __name__ == "__main__":
main()