1 public class MonteCarloSimulation { 2 public static void main(String[] args) { 3 final int NUMBER_OF_TRIALS = 10000000; 4 int numberOfHits = 0; 5 6 for (int i = 0; i < NUMBER_OF_TRIALS; i++) { 7 double x = Math.random() * 2.0 - 1; 8 double y = Math.random() * 2.0 - 1; 9 if (x * x + y * y <= 1) 10 numberOfHits++; 11 } 12 13 double pi = 4.0 * numberOfHits / NUMBER_OF_TRIALS; 14 System.out.println("PI is " + pi); 15 } 16 }