当前位置:网站首页>The Monte Carlo method solves the PI and draws points with turtle, and completes the progress bar problem
The Monte Carlo method solves the PI and draws points with turtle, and completes the progress bar problem
2022-07-28 05:37:00 【Programmer system】
Use Monte Carlo method to solve the PI π
Background of Monte Carlo method
'''
Monte Carlo method in 20 century 40 In the s, the United States developed the atomic bomb in the Second World War “ Mann
Hatton plan ” Members of the program S.M. Ulam and J. feng · Neumann first proposed . Mathematician Feng · Neumann uses the world-famous gambling city — Monaco's Monte Carlo— To name this method , Give it a layer of mystery . before this , Monte Carlo method already exists .1777 year , French mathematician Buffon (Georges Louis Leclere de Buffon,1707—1788) It is proposed to calculate the circumference ratio by means of needle throwing experiment π. This is considered to be the origin of Monte Carlo method .
'''
Subject requirements

The area of the unit circle is PI, The area of the circumscribed square of the unit circle is 4. therefore ,1/4 The area of a circle is PI/4, The area of the circumscribed square is 1 Use turtle library turtle.goto()、turtle.dot() Other methods , Complete the drawing as shown in the figure on the right ; And it is required that in the calculation process , Every time 10% When calculating the point of , Display the currently completed progress through console output .
This method is not difficult to understand , In fact, it's the probability problem learned in high school .
The code is as follows :
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: # Judge the point in the circle
hits += 1
#turtle.color("red") # You can also change the brush color , be not in dot Riga color
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 The value is {}.".format(pi))
print(" The running time is :{:.5f}s".format(clock()))
turtle.done()The operation results are as follows :

The middle dynamic progress bar is as follows :

Console :

Upper figure pi I forgot the value of line feed , But the code has changed , In the process of running, there will be the problem of slow dot drawing speed , It may be related to computers , I haven't found the reason yet , If a friend knows , You can send me a private letter , I will correct it immediately . Thank you for your support .
边栏推荐
猜你喜欢

repackag failed: Unable to find main class

Scanf function of input and output function in C language

Writing methods of scientific research papers: add analysis and discussion in the method part to explain their contributions and differences

Framework step by step easy-to-use process

冶金物理化学复习 --- 气-液相反应动力学

repackag failed: Unable to find main class

Thinking on multi system architecture design

visio如何快速生成相同的图案,生成图像矩阵

ByteBuffer. Position throws exception illegalargumentexception

ResNet结构对比
随机推荐
多模块打包:程序包:xxx不存在
21 day SQL punch in summary
restFul接口使用个人总结
First acquaintance with C language (1)
CentOS7安装MySQL5.7
Thinking on multi system architecture design
Low illumination image data set
openjudge:万年历
图像增强评价指标学习之——结构相似性SSIM
openjudge:校园食宿预订系统
block yandex bot
自定义Json返回数据
Use of IO streams
C language: some self realization of string functions
动态卷积的本质
openjudge:找第一个只出现一次的字符
JVM notes 3: class loading and bytecode Technology
多线程进阶:锁的策略
Mybats foreach multi select query, index loop, and cancel the and/or tag
Response<T>类