Calculation of Pi by Monte Carlo method
A few days ago read a blog: Introduction to Monte Carlo method,http://www.ruanyifeng.com/blog/2015/07/monte-carlo-method.html
It introduces the method of using probability to calculate pi, so the following attempts are made with the program.
The approximation of the PI value as a constant is 3.141592653589793 in Math.PI.
Ⅰ. Calculation of all pixels in a square
PackageYumu.probability.montecarlo; Public classCalculatepi {Private Static Final intRADIUS = 10000; Public Static voidMain (string[] args) {intCircle = 0; for(inti = 0; i < RADIUS; ++i) { for(intj = 0; J < RADIUS; ++j) { if(I*i + j*j < radius*RADIUS) { ++Circle; } } } DoubleQuarterpi = (Double) Circle/(RADIUS *RADIUS); System.out.println (Quarterpi* 4); } }
The results of the operation are as follows:
3.14199016
Ⅱ. Random pixel calculation in a square
PackageYumu.probability.montecarlo; Public classRandompi {Private Static Final intQUANTITY = 10000000; Public Static voidMain (string[] args) {Doublex, y; intCircle = 0; for(Longi = 0; i < QUANTITY; ++i) {x=Math.random (); Y=Math.random (); if(X*x + y*y < 1){ ++Circle; } } DoubleQuarterpi = (Double) Circle/QUANTITY; System.out.println (Quarterpi* 4); } }
The results of multiple runs are as follows:
3.14113443.1422143.1410763.1407648
Ⅲ. Averaging of random pixels in a square
PackageYumu.probability.montecarlo; Public classRandomtwicepi {Private Static Final intCOUNT = 100; Private Static Final intQUANTITY = 1000000; Public Static voidMain (string[] args) {Doublesum = 0; for(inti = 0; i < COUNT; ++i) {Sum+=Randompi (); } DoublePI = sum/COUNT; System.out.println (PI); } Private Static DoubleRandompi () {Doublex, y; intCircle = 0; for(Longi = 0; i < QUANTITY; ++i) {x=Math.random (); Y=Math.random (); if(X*x + y*y < 1){ ++Circle; } } DoubleQuarterpi = (Double) Circle/QUANTITY; returnQuarterpi*4; } }
The results of multiple runs are as follows:
3.14175815999999933.1414950800000013.1415940399999998
Ⅳ. Calculation of all small square bodies in a square body
PackageYumu.probability.montecarlo; Public classCalculatecubepi {Private Static Final intRADIUS = 1000; Public Static voidMain (string[] args) {intSphere = 0; for(inti = 0; i < RADIUS; ++i) { for(intj = 0; J < RADIUS; ++j) { for(intk = 0; K < RADIUS; ++k) { if(i*i + j*j + k*k < radius*RADIUS) { ++Sphere; } } } } DoubleONEINSIXOFPI = (Double) Sphere/(RADIUS * radius *RADIUS); System.out.println (ONEINSIXOFPI* 6); }}
The results of the operation are as follows:
3.148658436
Ⅴ. Leibniz formula calculation
Refer to this series: Leibniz formula forπ, https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80
PackageYumu.probability.montecarlo; Public classLeibnizseriespi {Private Static Final intCOUNT = 100000000; Public Static voidMain (string[] args) {DoubleQuarterpi = 1; for(inti = 1; i < COUNT; ++i) { Doubletemp = i%2==0? 1:-1; Temp/= i*2 + 1; Quarterpi+=temp; } System.out.println (Quarterpi* 4); }}
The results of the operation are as follows:
3.141592643589326
Ⅵ. Summary
In order to avoid more than 10 seconds of calculation time, the sample values are reduced at random.
"All pixel calculations in a square" calculates 10^8 times, and when you calculate the same number of times in the random pixel calculation in squares, you are caught waiting.
The reason for guessing is that it takes a lot of time to get a random number, or it can be too many times the number of cycles to consume.
"Averaging random pixels in a square" the CMB 10^8 is divided into calculated 10^6 for a common 10^2 averaging, and the computed value is obviously closer to the real pi than the "all-square-pixel calculation".
"All small-body calculations in the square body" may have entered a three-layer cycle that consumes time, and when RADIUS is 1000, the 10^9 is actually calculated, but the deviation is large.
The "Leibniz formula calculation" uses the formula PI/4 = 1-1/3 + 1/5-1/7 + 1/9 ...
Among them, "random pixel calculation in squares" and "random pixels averaging in squares" are randomly used and belong to the Monte Carlo method.
Using the Monte Carlo method, you can also get the nearest real value when the number of samples is very small, which saves computational resources more than all calculations.
Please click on " Follow me " below to follow me!
This address:http://www.cnblogs.com/kodoyang/p/MonteCarloMethod_PI.html
Rain Yoko
August 9, 2015
Monte Carlo Method calculates pi