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