// Monte Carlo calculation of Pi import java.util.Random; import java.io.*; class Pi { public static void main (String[] args) throws IOException { Random point = new Random(); int i,n,inside; float x,y; // Coordinates of a random point BufferedReader stdin; stdin = new BufferedReader (new InputStreamReader (System.in)); System.out.print ("Enter the number of random points: "); n = Integer.parseInt (stdin.readLine()); inside = 0; for (i = 1; i <= n; i++) { x = point.nextFloat()*2-1; // Generate a random point y = point.nextFloat()*2-1; if (x*x + y*y <= 1) inside++; // if inside the unit circle increment counter } System.out.print ("Pi = " + 4*(float)inside/n); } }