Recall the original Monte Carlo simulation: This is a slightly different version
ID: 3760279 • Letter: R
Question
Recall the original Monte Carlo simulation:
This is a slightly different version of it:
Some of the code has been reorganized so methods are called, but the methods are missing. You need to fill in the methods in the new version, so the new version will produce the same results as the old version.
There are two new methods in 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:
currX = newRand(-1, 1)
currY = 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).
The code for your two methods should go after the main program.
Explanation / Answer
Answer:
Two methods have been implemented.
Methods:
public static double newRand(double low, double high)
{
//generates random numbers
Random randomGena = new Random (System.currentTimeMillis());
//hist variable
int hitsVal = 0,numThrows1;
//initialised PI variable
double PI = 0;
for (int a = 1; a <= numThrows1; a++)
{
//getting random double values of low and high
double low = (randomGena.nextDouble()) * 2 - 1.0;
double high = (randomGena.nextDouble()) * 2 - 1.0;
// isInside of method
if (isInside(low, high))
{ //increments the hitsvalue method
hitsVal++;
}
}
double dthrows1 = numThrows1;
//PI value has been calculated
PI = (4.0 * (hitsVal/dthrows1));
return PI;
}
}
public static double findRadius(double x, double y)
{
//randomly getting the distances
double dist=Math.sqrt(x*x)+(y*y)
return (dist<1.0)
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.