一、产生随机数
import random
import time
random.seed(10) # 种子确定,数据有据可查,种子不同,产生的数据不能复现
for x in range(10):
print("非10的整数")
print(random.randint(10,100))
time.sleep(0.1)
print("10的整数")
print(random.randrange(10,100,10))
time.sleep(0.1)
二、蒙塔卡罗 方法计算 圆周率
import random
import time
hits = 0.0
DARTS = 1000*1000
start = time.perf_counter()
for i in range(1, DARTS+1):
x, y = random.random(), random.random()
dist = pow(x **2+y **2,0.5)
if dist <= 1.0:
hits += 1
pi=4 * (hits / DARTS)
print("圆周率的计算结果是:{}".format(pi))
print("所用的时间长度是:{:0.6f}s".format(time.perf_counter()-start))