当前位置:网站首页>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 .
边栏推荐
猜你喜欢

ByteBuffer. Position throws exception illegalargumentexception

JUC notes

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

架构设计思考之一(SSO设计)

【idea插件神器】教你如何使用IDEA一键set实体类中所有属性

论文模型主图范例

Thinking on multi system architecture design

Framework step by step easy-to-use process

Multi module packaging: package: XXX does not exist

BigDecimal rounds and retains two decimal places
随机推荐
Thesis writing function words
Oracle创建表、删除表、修改表(添加字段、修改字段、删除字段)语句总结
深度学习医学图像模型复现
Confused, I'm going to start running in the direction of [test]
MySQL practice 45 lectures
Multi module packaging: package: XXX does not exist
论文模型主图范例
Eccv2022 | 29 papers of Tencent Youtu were selected, including face security, image segmentation, target detection and other research directions
解决Oracle使用in语句不能超过1000问题
2022 summer practice (first week)
JUC notes
多模块打包:程序包:xxx不存在
(dark horse) MySQL beginner advanced notes (blogger lazy dog)
How about ink cloud?
Personal summary of restful interface use
Mabtis(一)框架的基本使用
visio如何精确控制图形的大小和位置及角度
Fusiongan code learning (I)
自定义Json返回数据
Openjudge: upper and lower case letters are interchanged