/*************************************************************************/
/* Program file name: Ex1_9.java                                         */
/*                                                                       */
/* © Tao Pang 2006                                                       */
/*                                                                       */
/* Last modified: June 7, 2006                                           */
/*                                                                       */
/* (1) This Java program is created for the book, "An Introduction to    */
/*     Computational Physics, 2nd Edition," written by Tao Pang and      */
/*     published by Cambridge University Press on January 19, 2006.      */
/*                                                                       */
/* (2) No warranties, express or implied, are made for this program.     */
/*                                                                       */
/*************************************************************************/

// Program for Exercise 1.9.  The value of pi is sampled by throwing
// a dart to a unit square with 0<x<1 and 0<y<1.  The Java random
// number generator is used.

import java.lang.*;
import java.util.Random;
public class Ex1_9 {
  final static int n = 1000000;
  public static void main(String argv[]) {
    Random r = new Random();

 // Throw the dart to the unit square
    int ic = 0;
    for (int i=0; i<n; ++i) {
      double x = r.nextDouble();
      double y = r.nextDouble();
      if ((x*x+y*y) < 1) ic++;
    }

 // Output the estimate of pi
    System.out.println("Estimated pi: " + (4.0*ic/n));
  }
}
