, pi); you can use a Monte Carlo method to estimate the value of the real constant ππ. Buffon's Needle is the name of a problem in which a needle is dropped on parallel lines. Measuring where the needle lands over and over again can be used to compute the value of pi. Consider a circle drawn within a square with a side of length 2r. We know that the area of the circle is πr2 and the area of the square is (2r)2 or 4r2. The ratio of the areas of the circle and square is πr2/4r2 or π/4. If we randomly throw darts at the square, we would expect some to land in the circle, and some to land in the square. If we take a ratio of the number of darts in the circle over the total number of darts, we would expect the ratio to equal π/4 To make the calculation easier, we can center a circle of radius 1 and square of size 2
I need to write a program to estimate the value of π using the Monte Carlo method described below. Print the result to 6 decimal places. I need to use System.out.printf ("%.6f%n", pi);
you can use a Monte Carlo method to estimate the value of the real constant ππ. Buffon's Needle is the name of a problem in which a needle is dropped on parallel lines. Measuring where the needle lands over and over again can be used to compute the value of pi. Consider a circle drawn within a square with a side of length 2r. We know that the area of the circle is πr2 and the area of the square is (2r)2 or 4r2. The ratio of the areas of the circle and square is πr2/4r2 or π/4. If we randomly throw darts at the square, we would expect some to land in the circle, and some to land in the square. If we take a ratio of the number of darts in the circle over the total number of darts, we would expect the ratio to equal π/4
To make the calculation easier, we can center a circle of radius 1 and square of size 2 at (0, 0). We can then just “throw darts” at the upper right quadrant. The expected ratio will still be pi/4. The more darts thrown, the more accurate the estimate.
To get a sequence of random number we use the Java random number generator defined by a class in the util package called Random. It is important sometimes, like for debugging, to get the same sequence of random numbers over and over again. In this way the results are repeatable. This is accomplished by initializing the random number generator with a number known as a "seed." The same seed will produced the same sequence of random numbers each time.I want to input the seed in the manner illustrated below.
private static final Random RNG = new Random (Long.getLong ("seed", System.nanoTime()));
Note: the above code is placed outside the main function but inside the class.
The seed is passed to the program using Java properties (sometimes they are called virtual machine arguments). These properties appear before the name of the class in the command line and have the from -Dname=value. See the examples below.
To get a random number between 0 and 1 use the random number generator in the following way:
double x = RNG.nextDouble(); // generate a random number between 0.0 and 1.0
double y = RNG.nextDouble(); // generate another random number between 0.0 and 1.0
Because of symmetry in this project, it does not matter if you randomly generate the x coordinate and then the y coordinate, or vice versa.
the program should take one command-line argument specifying the number of darts to use in the calculation. Here are some examples of running the program with a particular seed and the output the program produces:
java -Dseed=5463785 Needle 100 3.040000
java -Dseed=5463785 Needle 1000000000 3.141545
Code: import java.util.*; class Main{ public static void precisionCompute(int x, int y, int n) { if (y == 0) { System.out.print("Infinite"); return; } if (x == 0) { System.out.print("0"); return; } if (n <= 0) { System.out.print(x / y); return; } if (((x > 0) && (y < 0)) || ((x < 0) && (y > 0))) { System.out.print("-"); x = x > 0 ? x : -x; y = y > 0 ? y : -y; } int d = x / y; for (int i = 0; i <= n; i++) { System.out.print(d); x = x - (y * d); if (x == 0) break; x = x * 10; d = x / y; if (i == 0) System.out.print("."); } } public static void main(String[] args) { int x = 22, y = 7, n = 6; precisionCompute(x, y, n); } }
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 4 images