PiApproximation - double x = rgen.nextDouble(-1.0, +1.0);...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
/* * File: PiApproximation.java * -------------------------- * This progam computes an approximation to pi by simulating * a dart board, as described in the handout. The general * technique is called Monte Carlo integration. * * Author: CS1MD3, Nov. 2008 */ import acm.program.*; import acm.util.*; public class PiApproximation extends ConsoleProgram { public void run() { int inside = 0; for (int i = 0; i < NDARTS; i++) {
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: double x = rgen.nextDouble(-1.0, +1.0); double y = rgen.nextDouble(-1.0, +1.0); if (x * x + y * y &lt; 1.0) inside++; } double pi = 4.0 * inside / NDARTS; println(&quot;Pi is approximately &quot; + pi); } /* Private constant */ private static final int NDARTS = 10000; /* Number of darts to throw */ /* Private instance variables */ private RandomGenerator rgen = RandomGenerator.getInstance(); }...
View Full Document

This note was uploaded on 03/26/2011 for the course CS 1md3 taught by Professor Skip during the Spring '11 term at McMaster University.

Ask a homework question - tutors are online