Calculation of Pi by Monte Carlo method

Source: Internet
Author: User

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
Package Yumu.probability.montecarlo; public class Calculatepi {private static final int RADIUS = 10000; public static void Main (string[] args) {int circle = 0;                for (int i = 0, i < radius; ++i) {for (int j = 0; j < RADIUS; ++j) {if (I*i + j*j < Radius*radius) {++circle;        }}} Double Quarterpi = (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
 package yumu.probability.montecarlo; public class randompi { private  Static final int quantity = 10000000; public static void main ( String[] args)  { double x, y; int circle = 0; for (long  I = 0; i < quantity; ++i) {             x = math.random ();             y = math.random ();  if (x*x + y*y < 1) { ++circle;             }         } double quarterPI =  (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
package yumu.probability.montecarlo; public class randomtwicepi { private  static final int count = 100; private static final int  Quantity = 1000000; public static void main (String[] args)  {  Double sum = 0; for (Int i = 0; i < count; ++ i) {            sum += randompi ();         } double pi = sum / COUNT;         system.out.println (pi);     } private static  double randompi () { double x, y; int circle = 0; for (long  i = 0; i < quantity; ++i) {             X = math.random ();             y =  math.random ();  if (x*x + y*y < 1) { ++circle;             }        } double  quarterPI =  (Double) circle / quantity; return quarterpi*4;     }    }

The results of multiple runs are as follows:

3.14175815999999933.1414950800000013.1415940399999998

Ⅳ. Calculation of all small square bodies in a square body
Package yumu.probability.montecarlo; public class calculatecubepi { private  static final int radius = 1000; public static void main ( String[] args)  { int sphere = 0; for (int i = 0; i  < radius; ++i) { for (int j = 0; j < radius; ++j) {  for (int k = 0; k < radius; ++k) { if (i*i + j*j +  k*k < radius*radius) { ++sphere;                     }                 }             }        } double oneinsixofpi =   (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

Package Yumu.probability.montecarlo;  public class Leibnizseriespi {private static final int COUNT = 100000000; public static void Main (string[] args) {double Quarterpi = 1;            for (int i = 1; i < COUNT; ++i) {Double temp = 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

Calculation of Pi by Monte Carlo method

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.