Repository navigation
Expand file tree
/
Copy pathCalcPiwithMC.py
More file actions
77 lines (57 loc) · 1.53 KB
/
Copy pathCalcPiwithMC.py
File metadata and controls
77 lines (57 loc) · 1.53 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import random, math, time, threading
from multiprocessing import Queue
def CalcPI(NofS, q, lock):
lock.acquire()
Stime = time.time()
CountArea = 0
for i in range(NofS):
x = random.uniform(-1,1)
y = random.uniform(-1,1)
if x**2 + y**2 <= 1:
CountArea += 1
PiEst = CountArea*2*2/NofS
DurTime = time.time() - Stime
q.put([PiEst, DurTime])
lock.release()
if __name__ =='__main__':
Nofs = 10000000
lock = threading.Lock()
q = Queue()
t1 = threading.Thread(target=CalcPI, args=(Nofs,q,lock))
t2 = threading.Thread(target=CalcPI, args=(Nofs,q,lock))
t3 = threading.Thread(target=CalcPI, args=(Nofs,q,lock))
t4 = threading.Thread(target=CalcPI, args=(Nofs,q,lock))
t5 = threading.Thread(target=CalcPI, args=(Nofs,q,lock))
t6 = threading.Thread(target=CalcPI, args=(Nofs,q,lock))
t7 = threading.Thread(target=CalcPI, args=(Nofs,q,lock))
t8 = threading.Thread(target=CalcPI, args=(Nofs,q,lock))
t1.start()
t2.start()
t3.start()
t4.start()
t5.start()
t6.start()
t7.start()
t8.start()
t1.join()
t2.join()
t3.join()
t4.join()
t5.join()
t6.join()
t7.join()
t8.join()
qq = []
while not q.empty():
qq.append(q.get())
print(qq)
#CPE = CalcPI(50000000)
#
#print(CPE)
##print(100*(PiEst-math.pi)/math.pi)
#print(time.time() - Stime)