Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this code for this specific problem: public class MonteCarlo { public sta

ID: 3760725 • Letter: I

Question

I have this code for this specific problem:

public class MonteCarlo {

public static void main(String args[]) {
final int N = 5000;
int numInside = 0;
double X, Y, radius, mypi;

for (int i=0; i<N; i++) {
/* make a throw: get new x and new y */
X = newRand(-1., 1.);
Y = newRand(-1., 1.);

/* check if throw inside circle */
radius = findRadius(X, Y);
if (radius < 1.0) { /* increment counter if inside circle */
numInside++;
}
}
/* find ratio */
/* calculate pi */
mypi = 4*numInside / (float) N;
System.out.println("Pi = " + mypi);
}
}

In the above java code method is missing and we need to add below method.

There are two new methods we need to write for the new version. The first one is newRand():

   double newRand(double low, double high)

newRand() returns a random double in the range low to high. Hence, in the new version of the Monte Carlo simulation program, when we generate random x and y coordinates for a throw, the new statements are:

   X = newRand(-1, 1);

   Y = newRand(-1, 1);

The second method is findRadius():

   double findRadius(double x, double y)

findRadius() returns the distance of the point (x, y) from the point (0, 0).

AND THIS IS THE ANSWER:

public class AS_Monte_Carlo {

private static double newRand(double low,double high){
Random r = new Random();   
return Math.newRand (low + (low - high));
}
private static double findRadius(double x,double y){
return Math.sqrt(x*x + y*y);
}
public static void main(String args[]) {
final int N = 5000;
int numInside = 0;
double X, Y, radius, mypi;
for (int i=0; i<N; i++) {
/* make a throw: get new x and new y */
X = newRand(-1., 1.);
Y = newRand(-1., 1.);
/* check if throw inside circle */
radius = findRadius(X, Y);
if (radius < 1.0) { /* increment counter if inside circle */
numInside++;
}
}
/* find ratio */
/* calculate pi */
mypi = 4*numInside / (float) N;
System.out.println("Pi = " + mypi);
}
}

However, when you put this code into netBeans or any other software, the bolded statement is error and I do not know ho w to re-write the equatoin to give the correct result according to the example or question explained above - PLEASE HELP!

Explanation / Answer

Please find the required solution:

import java.util.Random;

public class AS_Monte_Carlo {

   // Method newRand gives random double from low to high
   private static double newRand(double low, double high) {
       Random random = new Random();
       return low + (high - low) * random.nextDouble();
   }// end of method newRand

   // Method that find the radius
   private static double findRadius(double x, double y) {
       return Math.sqrt(x * x + y * y);
   }// end of method findRadius

   public static void main(String args[]) {

       final int N = 5000;
       int numInside = 0;
       double X, Y, radius, mypi;
       for (int i = 0; i < N; i++) {

           /* make a throw: get new x and new y */
           X = newRand(-1., 1.);
           Y = newRand(-1., 1.);

           /* check if throw inside circle */
           radius = findRadius(X, Y);

           /* increment counter if inside circle */
           if (radius < 1.0) {
               numInside++;
           }
       }

       /* find ratio */
       /* calculate pi */
       mypi = 4 * numInside / (float) N;
       System.out.println("Pi = " + mypi);

   }// end of main method

}// end of class AS_Monte_Carlo

Output:

Pi = 3.1751999855041504