马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#利用随机数求积分
#对pi进行估计
import numpy as np
import math
class comput:
def __init__(self, n = 7000000):
self.n = n
def random_number2(self, seed = 1, m = pow(2, 31)-1, c = 19,a = pow(7, 5)):
res = []
xi_1 = seed
for i in range(self.n):
xi = (a * xi_1 + c) % m
res.append(xi / m)
xi_1 = xi
return np.array(res)
def comput_pi(self):
x = self.random_number2(seed = 23)
y = self.random_number2(seed = 61)
res_temp = pow((2*x-1), 2) + pow((2*y-1), 2)
res = np.sum(res_temp <= 1) / self.n
return res*4
res = comput().comput_pi()
print(res)
参考:
1.Sheldon M.Ross《统计模拟》 |