// Calculate Pi by the Monte Carlo method. Draws the random point area import java.applet.Applet; import java.awt.*; import java.util.Random; public class Pi1 extends Applet { Random point = new Random(); int i = 0; int n = 10000; // set the number of random points int inside = 0; double x, y; // Coordinates of a random point public void paint (Graphics page) { do { i++; x = point.nextDouble()*2-1; // Generate a random point y = point.nextDouble()*2-1; page.fillRect ((int)(x*100)+150, (int)(y*100)+150, 1, 1); if (x*x + y*y <= 1) inside++; // if inside the unit circle increment counter } while (i < n); page.drawString ("Pi = " + String.valueOf (4*(float)inside/n), 50, 270); } }