1 #CalPi.py 2 from random import random 3 from math import sqrt 4 from time import clock 5 DARTS = 10000000 6 hits = 0.0 7 clock() 8 for i in range(1, DARTS+1): 9 x, y = random(), random() 10 dist = sqrt(x ** 2 + y ** 2) 11 if dist <= 1.0: 12 hits = hits + 1 13 pi = 4.0 * (hits/DARTS) 14 print("pi值是{}".format(pi)) 15 print("运行时间是:{:5.5}s".format(clock()))