python 学习 蒙特卡洛求π DAY9

import random 
import math 
import time


s = eval(input("请输入取点总数:"))
hits = 0.0
start = time.perf_counter()
for i in range(s):
    x,y = random.random(),random.random()
    dist = math.sqrt(x**2 + y**2)
    if dist <= 1.0:
        hits += 1
    else:
        continue
pi = hits/s*4
t = time.perf_counter() - start
print("pi是{0},运行时间是{1:5.5f}s".format(pi,t))

猜你喜欢

转载自blog.csdn.net/u011451186/article/details/81015602