• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
汤圆喵喵
博客园    首页    新随笔    联系   管理    订阅  订阅
用phthon计算圆周率
from math import sqrt
from tqdm import tqdm   #导入进度条
from random import random  #导入随机函数
import time  #导入时间包
DARTS=100
hits=0.0
t=time.perf_counter()
# 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}%[{}->{}]{:.100f}s".format(c,a,b,-t),end='')
print("运行时间是:{:.5f}s".format(time.perf_counter()))

c=pi*d=2*pi*r

pi=C/d

 

posted on 2020-10-01 23:56  汤圆喵喵  阅读(130)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3