Python第三次作业

计算圆周率pi

 1 from random import random 
 2 from tqdm import trange
 3 import time 
 4 DARTS = 10000*10000
 5 hits  = 0.0
 6 
 7 for i in range(1 , DARTS+1):
 8     x , y = random() , random()
 9     dist = pow(x**2 + y**2, 0.5)
10     if dist <= 1.0:
11         hits = hits + 1 
12 pi = 4 * (hits/DARTS)                  #蒙德卡罗方法求 pi
13 
14 print("计算中...")
15 for i in trange(100):
16     time.sleep(0.1)
17 print(pi)

 

posted @ 2020-03-21 23:50  玩的三立方  阅读(192)  评论(0编辑  收藏  举报