圆周率计算

from math import sqrt
from tqdm import tqdm
from random import random
import time
DARTS=10000
hits=0.0
t=time.perf_counter()
for i in tqdm(range(1,DARTS+1)):
  x,y=random(),random()
  dist=pow(x**2+y**2, 0.5)
  if dist<=1.0:
    hits+=1
  a='*'*i
  b='.'*(DARTS+1-i)
  c=(i/DARTS+1)*100
  t-=time.perf_counter()
  time.sleep(0.00001)
pi=4*(hits/DARTS)
print("Pi值是{}.".format(pi))
print("\t{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,-t),end='')
print("运行时间是:{:.5f}s".format(time.perf_counter()))

 

posted @ 2020-12-14 19:03  大兔xx  阅读(118)  评论(0)    收藏  举报