1 from random import random 2 import time 3 from time import perf_counter 4 darts=10000*10000 5 hits=0.0 6 scale=10 7 start=perf_counter() 8 for j in range(scale+1): 9 a='*'*j 10 b='.'*(scale-j) 11 c=(j/scale)*100 12 dur=time.perf_counter()-start 13 print("{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur)) 14 time.sleep(0.1) 15 for i in range(1,darts+1): 16 x,y=random(),random() 17 dist=pow(x**2+y**2,0.5) 18 if dist<=1.0: 19 hits=hits+1 20 pi=4*(hits/darts) 21 print("/n"+"圆周率的值是{}".format(pi))
来源:https://www.cnblogs.com/qinlai/p/12567149.html