/************************************************************** * Monte Carlo Simulation to calculate Pi * Mark Goadrich * CSC 204 - Spring 2008 **************************************************************/ public class Pi{ // Main method to run the program public static void main(String[] args) { // declare our counter variables int i = 0; int c = 0; // loop 1000 times while (i < 1000) { // calculate x and y points between -1 and 1 double x = (Math.random() * 2) - 1; double y = (Math.random() * 2) - 1; // if point is in unit circle, count it if (Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) <= 1) { c++; } i++; } System.out.println("Our estimate of Pi is " + (((double)c / i) * 4)); } }