Calculation of π

Source: Internet
Author: User
Tags value of pi

Pi is an irrational number, and there is no precise formula to calculate the π value, and π can only be calculated using approximate algorithms.

The internationally recognized PI values are calculated using the Monte Carlo method.

Monte Carlo (Monte Carlo) method, also known as random sampling or Statistical test method.

When the problem solved is the probability of an event, or the expected value of a random variable, it can be solved by some kind of "experiment" method.

That is, Monte Carlo is a method to solve problems by using random experiments .

Monte Carlo method

? Monte Carlo method provides a way to solve The problem of solving problems in reality by using random numbers and random experiments in computer.

? It is widely used in Financial engineering, Macroeconomics, computational physics (such as particle transport calculations, quantum thermodynamic calculations, aerodynamic

Computing) and other fields.

The Monte Carlo method solves π 's steps:

  First construct a unit square and 1/4 Circle

  

Randomly throws a large number of points to the unit square and circular structure, for each point, may be within the circle or outside the circle, when the random number of points up to

To a certain extent, the circle will make up the area of the circle, and all the points will make up the rectangular area. The number of points divided by the number of points in the circle is

Than, that is, Π/4. The greater the number of random points, the more accurate the PI value.

The IPO of the π calculation problem is as follows:

? Input: Number of throw points

? Handling: For each scatter point, calculate the distance from the point to the center of The Circle, judging by the distance that the point is inside or out of the circle.

Count the number of points within a circle

? Output: π value

1 #pi.py2  fromRandomImportRandom3  fromMathImportsqrt4  fromTimeImportClock5DARTS = 12006hits =07 clock ()8 ## #核心代码9  forIinchRange (1, DARTS):TenX, y = random (), random ()#random () gives the stochastic coordinate value (x, y) Onedist = sqrt (x**2 + y**2)#sqrt () calculates the distance from the throw point to the origin. A     ifDist <= 1.0:#Judging whether it falls within the circle -hits = hits + 1 -PI = 4 * (hits/DARTS) the ### - Print("The value of pi is%s"%pi) - Print("program run time is%-5.5SS"% Clock ())

Results:

  

? Because the number of darts points is small, the value of π is not very accurate.

Calculation of π

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.