**Ok, I am having problems with my code, I am a newbie but I think I am close to the solution. here is the assignment:**

In geometry the ratio of the circumference of a circle to its diameter is known as π. The value of π can be estimated from an infinite series of the form:

π / 4 = 1 - (1/3) + (1/5) - (1/7) + (1/9) - (1/11) + ...

There is another novel approach to calculate π. Imagine that you have a dart board that is 2 units square. It inscribes a circle of unit radius. The center of the circle coincides with the center of the square. Now imagine that you throw darts at that dart board randomly. Then the ratio of the number of darts that fall within the circle to the total number of darts thrown is the same as the ratio of the area of the circle to the area of the square dart board. The area of a circle with unit radius is just π square unit. The area of the dart board is 4 square units. The ratio of the area of the circle to the area of the square is π / 4.

To simuluate the throwing of darts we will use a random number generator. The Math class has a random() method that can be used. This method returns random numbers between 0.0 (inclusive) to 1.0 (exclusive). There is an even better random number generator that is provided the Random class. We will first create a Random object called randomGen. This random number generator needs a seed to get started. We will read the time from the System clock and use that as our seed.

Random randomGen = new Random ( System.currentTimeMillis() );

Imagine that the square dart board has a coordinate system attached to it. The upper right corner has coordinates ( 1.0, 1.0) and the lower left corner has coordinates ( -1.0, -1.0 ). It has sides that are 2 units long and its center (as well as the center of the inscribed circle) is at the origin.

A random point inside the dart board can be specified by its x and y coordinates. These values are generated using the random number generator. There is a method nextDouble() that will return a double between 0.0 (inclusive) and 1.0 (exclusive). But we need random numbers between -1.0 and +1.0. The way we achieve that is:

double xPos = (randomGen.nextDouble()) * 2 - 1.0;

double yPos = (randomGen.nextDouble()) * 2 - 1.0;

To determine if a point is inside the circle its distance from the center of the circle must be less than the radius of the circle. The distance of a point with coordinates ( xPos, yPos ) from the center is Math.sqrt ( xPos * xPos + yPos * yPos ). The radius of the circle is 1 unit.

The class that you will be writing will be called CalculatePI. It will have the following structure:

import java.util.*;

public class CalculatePI

{

public static boolean isInside ( double xPos, double yPos )

{ ... }

public static double computePI ( int numThrows )

{ ... }

public static void main ( String[] args )

{ ... }

}

In your method main() you want to experiment and see if the accuracy of PI increases with the number of throws on the dartboard. You will compare your result with the value given by Math.PI. The quantity Difference in the output is your calculated value of PI minus Math.PI. Use the following number of throws to run your experiment - 100, 1000, 10,000, and 100,000. You will call the method computePI() with these numbers as input parameters. Your output will be of the following form:

Computation of PI using Random Numbers

Number of throws = 100, Computed PI = ..., Difference = ...

Number of throws = 1000, Computed PI = ..., Difference = ...

Number of throws = 10000, Computed PI = ..., Difference = ...

Number of throws = 100000, Computed PI = ..., Difference = ...

* Difference = Computed PI - Math.PI

In the method computePI() you will simulate the throw of a dart by generating random numbers for the x and y coordinates. You will call the method isInside() to determine if the point is inside the circle or not. This you will do as many times as specified by the number of throws. You will keep a count of the number of times a dart landed inside the circle. That figure divided by the total number of throws is the ratio π / 4. The method computePI() will return the computed value of PI.

**Here is what I have done so far but the value of PI is coming out 0.0 and the difference is coming out the negative value of PI (-3.141592....) I having been debugging for the last day and a half. I've kinda have lost the ability to be objective about this and was wondering what anyone though about what I might be doing wrong?**

import java.util.*; public class CalculatePI2 { public static boolean isInside (double xPos, double yPos) { boolean result; double distance = Math.sqrt((xPos * xPos) + (yPos * yPos)); if (distance < 1) result = false; return(distance < 1); } public static double computePI (int numThrows) { Random randomGen = new Random (System.currentTimeMillis()); int hits = 0; double PI = 0; for (int i = 0; i <= numThrows; i++) { double xPos = (randomGen.nextDouble()) * 2 - 1.0; double yPos = (randomGen.nextDouble()) * 2 - 1.0; if (isInside(xPos, yPos)) { hits++; } PI = (4 * (hits/numThrows)); } return PI; } public static void main (String[] args) { Scanner reader = new Scanner (System.in); System.out.println("This program approximates PI using the Monte Carlo method."); System.out.println("It simulates throwing darts at a dartboard."); System.out.print("Please enter number of throws: "); int numThrows = reader.nextInt(); double PI = computePI(numThrows); double Difference = PI - Math.PI; System.out.println ("Number of throws = " + numThrows + ", Computed PI = " + PI + ", Difference = " + Difference ); } }

**I actually made the program to where the user could enter the number of throws and I really only needed to have the program approximate Pi for the values of 100, 1000, 10000, and 100000. Not sure if he is going to like that.**

This post has been edited by **KoreyAusTex**: 10 July 2007 - 05:20 PM