Π

#CalPiV2.py
import random
import time
N=1000*1000
k=0
start=time.perf_counter()
for i in range(N):
x,y=random.random(),random.random()
dist=pow(x**2+y**2,0.5)
if dist<=1.0:
k+=1
pi=4*(k/N)
print("pi={},run time={:.2f}s".format(pi,time.perf_counter()-start))

 

posted @ 2022-10-19 18:05  Camille_xx  阅读(192)  评论(0)    收藏  举报