鱼C论坛

 找回密码
 立即注册
查看: 1853|回复: 0

[技术交流] python实现统计模拟【第3章】【利用随机数求积分】

[复制链接]
发表于 2022-1-30 14:45:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #利用随机数求积分
  2. #对pi进行估计
  3. import numpy as np
  4. import math

  5. class comput:
  6.     def __init__(self, n = 7000000):
  7.         self.n = n
  8.    
  9.     def random_number2(self, seed = 1, m = pow(2, 31)-1, c = 19,a = pow(7, 5)):   
  10.         res = []
  11.         xi_1 = seed
  12.         for i in range(self.n):
  13.             xi = (a * xi_1 + c) % m
  14.             res.append(xi / m)
  15.             xi_1 = xi
  16.         return np.array(res)
  17.    
  18.     def comput_pi(self):
  19.         x = self.random_number2(seed = 23)
  20.         y = self.random_number2(seed = 61)
  21.         res_temp = pow((2*x-1), 2) + pow((2*y-1), 2)
  22.         res = np.sum(res_temp <= 1) / self.n
  23.         return res*4

  24. res = comput().comput_pi()
  25. print(res)
复制代码



参考:
1.Sheldon M.Ross《统计模拟》

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 13:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表