random随机数

•random的使用

 

 

 

 

 

 

 

 

import random as ran
import time as t
ran.seed(123)
darts = eval(input())
hits = 0.0
start = t.perf_counter()
for i in range(1,darts+1):
    x,y = ran.random(),ran.random()
    dist = pow(x**2+y**2,0.5)
    if dist <=1.0: #利用到圆心的距离判断(x,y)是否在圆内
        hits += 1
pi = 4 * (hits/darts)
print("圆周率值是:{}".format(pi))
print("运行时间是:{}s".format(t.perf_counter()-start))

 

posted @ 2021-01-30 15:34  MMMMinoz  阅读(88)  评论(0编辑  收藏  举报