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