import java.util.Random; import java.text.NumberFormat; public class l8 { public static void main(String[] args) { final int POINT = 2; double cX = 1.00000000, cY = 1.00000000; double y = 0.00000000, x = 0.00000000, hit = 0.00000000, miss = 0.00000000; double estPI = 0.0; double mathPI = Math.PI/4; Random generator = new Random(); NumberFormat formatter = NumberFormat.getNumberInstance(); formatter.setMaximumFractionDigits(9); for(int i = 1; i <= 20000; i++) { x = generator.nextDouble() * 2; y = generator.nextDouble() * 2; //System.out.print("x: " + x + " y: " + y + "\t"); double length = Math.sqrt(Math.pow(cX-x,2) + Math.pow(cY-y,2)); if(length <= 1.0) { hit++; } else { miss++; } estPI = (4 * hit)/i; if((i%1000)==0) { //System.out.print("Hits: " + hit + " Miss: " + miss + " Total: " + i); System.out.println("After " + i + " darts the estimated value of PI is " + formatter.format(estPI)); } } System.out.println("\n\nComputer Value of PI for Math.PI is " + Math.PI); double diff = Math.abs(Math.PI - estPI); System.out.println("The absolute value difference between Math.PI and my final estimate value for PI is " + diff); } }