当前位置:网站首页>蒙特卡罗方法求解圆周率π并用turtle画点,以及完成进度条问题
蒙特卡罗方法求解圆周率π并用turtle画点,以及完成进度条问题
2022-07-28 05:18:00 【编程器系统】
用蒙特卡罗方法求解圆周率π
蒙特卡罗方法的背景
'''
蒙特卡罗方法于 20 世纪 40 年代美国在第二次世界大战中研制原子弹的“曼
哈顿计划”计划的成员 S.M.乌拉姆和 J.冯·诺伊曼首先提出。数学家冯·诺伊曼用驰名世界的赌城—摩纳哥的 Monte Carlo—来命名这种方法,为它蒙上了一层神秘色彩。在这之前,蒙特卡罗方法就已经存在。1777 年,法国数学家布丰(Georges Louis Leclere de Buffon,1707—1788)提出用投针实验的方法求圆周率π。这被认为是蒙特卡罗方法的起源。
'''
题目要求

单位圆的面积是 PI,单位圆的外切正方形面积是 4。所以,1/4 个圆的面积是 PI/4,外切正方形的面积是 1使用 turtle 库 turtle.goto()、turtle.dot()等方法,完成如上右图所示的图形绘制;并要求在计算过程中,每完成 10%的点的计算时,通过控制台输出显示当前完成的进度。
该方法不难理解,其实就是高中所学过的概率问题。
代码如下:
import time
from random import random
from math import *
from time import clock
from time import sleep
import turtle
import sys
DARTS = 1000
hits = 0
clock()
turtle.setup(800, 800,startx=0,starty=0)
turtle.speed(0)
turtle.pensize(2)
for i in range(1, DARTS + 1):
x, y = random(), random()
dist = sqrt(x ** 2 + y ** 2)
turtle.penup()
turtle.goto(x*200,y*200)
turtle.pendown()
if dist <= 1.0: # 判断位于圆内的点
hits += 1
#turtle.color("red") #也可改变画笔颜色,不在dot里加颜色
turtle.dot(6,"red")
else:
#turtle.color('blue')
turtle.dot(6,'blue')
j=int(i/10)
if j%10==0:
print("\r",end="")
print("Download progress:{}%".format(j)," "*(j//2),end="")
sys.stdout.flush()
time.sleep(0.01)
pi = 4 * (hits / DARTS)
print("\n")
print("pi值是{}。".format(pi))
print("运行时间是:{:.5f}s".format(clock()))
turtle.done()运行结果如下:

中间动态进度条如下:

控制台中:

上图pi的值忘了该换行了,不过代码已经更改过了,在运行过程中会出现画点速度较慢的问题,可能与电脑有关,我暂时还没找到原因,如果有朋友知道,可以私信给我,我会立即纠正。谢谢大家支持。
边栏推荐
- JSON in JS (launch object deep copy)
- 7. < tag string and API trade-offs> supplement: Sword finger offer 05. replace spaces
- About localdatetime in swagger
- 集合框架的操作使用
- Problems encountered when the registry service Eureka switches to nocas
- YUV to uiimage
- IDEA配置 service(Run Dashboard) 服务,多模块同时启动
- ByteBuffer. Position throws exception illegalargumentexception
- C language: realize the simple function of address book through structure
- 【SLAM】LVI-SAM解析——综述
猜你喜欢
![[computer level 3 information security] overview of information security assurance](/img/f0/a72e61fda58ea93ca4e9db7274f6e3.png)
[computer level 3 information security] overview of information security assurance

VMware Workstation 与 Device/Credential Guard 不兼容。禁用 Device/Credential Guard

框架一步一步方便使用的流程

First acquaintance with C language (1)

ECCV22 最新54篇论文主图整理

科研论文写作方法:在方法部分添加分析和讨论说明自己的贡献和不同

Edge calculation kubeedge+edgemash

2022 summer practice (PowerDesigner tutorial learning record) (first week)

IDEA使用dev-tool实现热部署

个人写的一个文件上传工具网站
随机推荐
Response<T>类
I interviewed a 38 year old programmer and refused to work overtime
Making RPM packages with nfpm
Offline loading of wkwebview and problems encountered
Invalid bound statement (not found): com.exam.mapper.UserMapper.findbyid
JUC笔记
Confused, I'm going to start running in the direction of [test]
Bean的作用域、执行流程、生命周期
多线程进阶:锁的策略
Mysql处理遗留数据样例
C language: some self realization of string functions
FeignClient 调用GET 方法报错 ResultVO{result=未知异常,异常详情:Request method ‘POST‘ not supported
关于swagger中的localDateTime
论文模型主图范例
Localdatetime removes T, and jsonfield is invalid
Struct模块到底有多实用?一个知识点立马学习
regular expression
First acquaintance with C language (2)
论文写作用词
Advanced assignment method of ES6 -- Deconstruction assignment