1 from tqdm import tqdm 2 import time 3 from random import random 4 from math import sqrt 5 DARTS=100000000 6 hits=0.0 7 time.clock() 8 for i in range(1,DARTS): 9 x,y=random(),random() 10 dist=sqrt(x**2+y**2) 11 if dist<=1.0: 12 hits=hits+1 13 for i in tqdm(range(1,DARTS//100000+1)): 14 time.sleep(0.05) 15 pi=4*(hits/DARTS) 16 print("Pi值是{}.".format(pi)) 17 print("运行时间是:{:.5f}s".format(time.clock()))