Π
#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))


浙公网安备 33010602011771号