-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmcpi.py
45 lines (32 loc) · 976 Bytes
/
mcpi.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
39
40
41
42
43
44
45
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
import math
from time import perf_counter
n_tosses = 10000
#<---- start timing
start = perf_counter()
x = np.random.rand(n_tosses)
y = np.random.rand(n_tosses)
n_in_circle = 0
for i in range(0, n_tosses - 1):
if (x[i]**2 + y[i]**2 <= 1):
n_in_circle += 1
end = perf_counter()
#<---- end timing
pi_estimate = 4 * (n_in_circle / n_tosses)
print("Monte-Carlo Pi Estimator")
print("Estimated π: ", pi_estimate)
print("Percent error: ", 100 * math.fabs(math.pi - pi_estimate) / math.pi, "%")
print("Elapsed time: ", end - start, "seconds")
circle_x = x[np.sqrt(x**2 + y**2) <= 1]
circle_y = y[np.sqrt(x**2 + y**2) <= 1]
fig = plt.figure()
plot = fig.add_subplot(111)
plot.scatter(x, y, marker='.', color='blue')
plot.scatter(circle_x, circle_y, marker='.', color='red')
x = np.linspace(0, 1, 100)
y = np.sqrt(1 - x**2)
plot.plot(x, y, color='black')
plot.set_aspect(1.0)
plt.show()