, 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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

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

Expert Solution
Step 1

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

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 4 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY