-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculoPiGit.py
More file actions
32 lines (26 loc) · 900 Bytes
/
calculoPiGit.py
File metadata and controls
32 lines (26 loc) · 900 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
import numpy as np
from timeit import default_timer as timer
def inside(p):
"""
Generate a random point and check if the point is within the circle with radius=1.
Returns:
(bool) whether the generated point is within the circle area
"""
x, y = np.random.random(), np.random.random()
return x*x + y*y < 1
def estimate_pi(num_samples):
"""
Estimate the value of Pi by means of repeated sampling. Benchmark the repeated sampling time cost.
Params:
num_sampes (int): the number of sample points to generate
Returns:
(float) estimated value of Pi
"""
print("Executing Spark job...")
start = timer()
dots = list(filter(inside,list(range(num_samples))))
count = len(dots)
end = timer()
print("Spark job ended. Total time elapsed: %s" % (end-start))
return(4.0 * count / num_samples)
estimate_pi(5000000)