第四周作业用python计算圆周率PI

发布时间 2023-11-15 09:49:05作者: 疯了快疯了
from math import sqrt
from tqdm import tqdm
from random import random
import time
DARTS=10000
hits=0.10000
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()))