PHP is the most stupid but interesting method to implement the PI method for projection.

Source: Internet
Author: User
PHP is the most stupid but interesting method to implement the PI method for projection.
#### Principle: PI is obtained by using the method of throwing darts! [Circle pi] (http://static.oschina.net/uploads/img/201408/30103044_VJtv.jpg) #### The following summary is selected from other users: 1. Figure2 is part of the top right corner of figure1. 2. throw a dart to Figure2 several times (a large number), and each time it is still at a different point. 3. if the number of throws is very high, Figure2 will be stabbed to be "riddled with holes ". 4. at this time, the "number of throwing in the circle" is divided by the "total throwing times" and multiplied by 4, which is the value of PI! (For details about the derivation process, refer to the original article.) in this algorithm, it is very important to know how to "randomly throw at Figure2 ", that is to say, how to ensure that the probability of each vertex in Figure2 is equal. Someone has concluded that this is actually called the Monte Carlo algorithm. if we take a square (1x1) in a unit as an incircle (unit circle), then the area of the square is: inner cut unit circle area = number of darts in the unit square: Number of darts in the inner cut unit circle. by calculating the number of darts, you can calculate the area of the unit circle and calculate the circumference rate. Note that the accuracy is proportional to the number of darts you throw. #### PHP source code implementation: the mt_rand random function provided by PHP has a large deviation and is replaced by Halton sequence. for the test results, see the following ~~~. Php
 0) {$ f/= $ base; $ result + = $ f * ($ I % $ base); $ I = floor ($ I/$ base );} return $ result ;}~~~ In the source code, the input parameters of halton are adjusted several times, which is more accurate. the error in testing PI = 3.14156mt _ rand is large. the result of the three Tests is as follows: mt_rand-1 = & gt; 3.142904mt _ rand-2 & gt; 3.143196mt _ rand-3 & gt; 3.139312

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.