I'm very new to Java and we have a project to calculate PI. Here is my prompt:

Determining π Experimentally:

Recall that π is the ratio of a circle's circumference to its

diameter and that we can calculate the area of a circle with the

formula A=πr2. Below is a circle enscribed within the unit

square.

What is the ratio of the areas of the enscribed circle to that of

the unit square?

If we pick a random point within the unit square what is the

probability that the point will also lie within the circle?

If we repeat this experiment an arbitrarily large number of times the

ratio of the number of points which lie within the circle to the number

of points within the unit square (all of them) will approach π/4.

Using the language structures we have discussed write a program that

will do the above experiment an arbitary (determined at run-time)

number of times and report back the approximate value of π.

---------------------------------------------------------------------------------

I did the program but for some reason, everytime I run it, I get a different value even if I type in the same number of repeats. Here is my code:

import java.util.*; public class PI { public static boolean isInside (double x1, double y1) { double distance = Math.sqrt((x1 * x1) + (y1 * y1)); return (distance < 1.0); } public static double computePI (int numRepeats) { Random randomGen = new Random (System.currentTimeMillis()); int repeats = 0; double PI = 0; for (int i = 1; i <= numRepeats; i++) { double x1 = (randomGen.nextDouble()) * 2 - 1.0; double y1 = (randomGen.nextDouble()) * 2 - 1.0; if (isInside(x1, y1)) { repeats++; } } double dRepeats = numRepeats; PI = (4.0 * (repeats/dRepeats)); return PI; } public static void main (String[] args) { Scanner scan = new Scanner (System.in); System.out.print("Please enter number of times you would like to run this program: "); int numRepeats = scan.nextInt(); double PI = computePI(numRepeats); System.out.println ("Computed PI = " + PI); } }

I'm really confused because I don't understand what I'm doing wrong. If someone could help me, I'd really appreciate it!