PiEstimator.java

  1. package org.drip.zen.juice;

  2. /*
  3.  * 1) Introduce Probability
  4.  * 2) Motivate the Lesson
  5.  * 3) Outline the Approach
  6.  * 4) Write Down the Formula
  7.  * 5) Random Numbers Between -1 and 1
  8.  * 6) Is Inside Circle
  9.  * 7) Count of Inside Circle
  10.  * 8) Probability To PI
  11.  * 9) Generate Random Numbers
  12.  * 10) Bring them all together
  13.  */

  14. public class PiEstimator {

  15.     static double RandomNumber()
  16.     {
  17.         double random = Math.random();

  18.         double randomBetweenMinus1AndPlus1 = 2. * random - 1;

  19.         return randomBetweenMinus1AndPlus1;
  20.     }

  21.     static boolean IsPointInsideCircle (double x, double y)
  22.     {
  23.         double distanceFromOrigin = Math.sqrt (x * x + y * y);

  24.         boolean insideCircle = false;

  25.         if (distanceFromOrigin < 1.)
  26.         {
  27.             insideCircle = true;
  28.         }
  29.         else
  30.         {
  31.             insideCircle = false;
  32.         }

  33.         return insideCircle;
  34.     }

  35.     static double InsideCircleCount (double[] xArray, double[] yArray)
  36.     {
  37.         double numberInsideCircle = 0.;
  38.         int numberOfPoints = xArray.length;

  39.         for (int counter = 0; counter < numberOfPoints; counter = counter + 1)
  40.         {
  41.             if (IsPointInsideCircle (xArray[counter], yArray[counter]))
  42.             {
  43.                 numberInsideCircle = numberInsideCircle + 1;
  44.             }
  45.         }

  46.         return numberInsideCircle;
  47.     }

  48.     static double ProbabilityToPI (double probability)
  49.     {
  50.         double pi = 4. * probability;
  51.         return pi;
  52.     }

  53.     public static void main (String[] input)
  54.     {
  55.         int totalNumberOfPoints = 10000000;

  56.         double[] xPoints = new double[totalNumberOfPoints];
  57.         double[] yPoints = new double[totalNumberOfPoints];

  58.         for (int pointCounter = 0; pointCounter < totalNumberOfPoints; pointCounter = pointCounter + 1)
  59.         {
  60.             xPoints[pointCounter] = RandomNumber();
  61.             yPoints[pointCounter] = RandomNumber();
  62.         }

  63.         double pointsInsideCircle = InsideCircleCount (xPoints, yPoints);

  64.         double probabilityOfPointsInsideCircle = pointsInsideCircle / totalNumberOfPoints;

  65.         double estimatedPI = ProbabilityToPI (probabilityOfPointsInsideCircle);

  66.         System.out.println ("\tNumber of Points Inside Square    : " + totalNumberOfPoints);

  67.         System.out.println ("\tNumber of Points Inside Circle    : " + pointsInsideCircle);

  68.         System.out.println ("\tProbability Of Points in a Circle : " + probabilityOfPointsInsideCircle);

  69.         System.out.println ("\tEstimated PI                      : " + estimatedPI);

  70.         System.out.println ("\tActual PI                         : " + Math.PI);
  71.     }
  72. }