Question

Write a Java program that uses the Monte Carlo method to estimate the value of PI....

Write a Java program that uses the Monte Carlo method to estimate the value of PI.

This method uses the unit circle inscribed in a square with sides of length 2 and random numbers to perform the estimation.

The estimation works as follows:

• Two random numbers are generated during each iteration of a loop.

• The random numbers are each in the range of -1 to 1. One random number is the x-coordinate and the other is the y-coordinate. Therefore the point generated by these two coordinates lies inside the square.

• Some of the points generated will lie on or inside the circle. This will be true if the following condition holds:

sqrt(x2 + y2) <= 1.

• PI can be estimated by looking at the ratio of the points generated that lie on or inside the circle to the total number of points generated (some of which will lie outside the circle).

• The law of large numbers suggests that as the number of iterations (i.e number of points generated) becomes large, the above ratio will approach the ratio of the area of the circle to the area of the square, that is:

As number of trials approaches ∞,

(points on or inside circle / total points generated) approaches (area of circle)/(area of square).

• Since the area of the unit circle is PI units and the area of the square is 4 units Pi can be estimated as follows:

(points on or inside circle)/(total points) = (area of circle)/(area of square)

​​​​​= PI / 4

so that,

​​PI = 4 * (points on or inside circle)/total points.

Your program should do the following:

• Ask the user to input a positive integer for the number of trials to be used for the estimation.

• Estimate PI using the Monte Carlo method. Note: You MUST use Math.random() to generate your random values.

• Output the estimate of PI.

• Ask the user if they would like to try another estimation.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

thanks for the question, here is the completed code in Java with inline comments, the larger the value of SIZE the more close the value of PI will be calculated, here is the code

==============================================================================================

import java.util.Random;



public class MonteCarlo {



    public static void main(String[] args) {

        Random random = new Random();

        // running the problem for 1Million times

        int SIZE = 1000000;

        double countWithinCircle = 0;



        for (int i = 1; i <= SIZE; i++) {

            // generating a random number between -1 and 1

            double x = -1 + random.nextDouble()*2;

            double y = -1 + random.nextDouble()*2;

            // checking if the distance is less than equal to the radius of the unit circle

            // if yes we count it as within the circle

            if (Math.sqrt(x * x + y * y) <= 1) {

                countWithinCircle += 1;

            }



        }

        // to calculate the value of PI we need to multiply the ratio with 4

        System.out.println("Value of PI as calculated: "+ 4*countWithinCircle/SIZE);





    }

}

======================================================================================

Add a comment
Know the answer?
Add Answer to:
Write a Java program that uses the Monte Carlo method to estimate the value of PI....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use Monte Carlo Simulation to approximate pi by considering the percentage of points with coordinates (x,y)...

    Use Monte Carlo Simulation to approximate pi by considering the percentage of points with coordinates (x,y) generated randomly by choosing x and y both between 0 and 1, inside the unit quarted circle Q: X^2 + y^2 = 1, x>=0 y>=0 when the quarter circle is taken to be inside the square S: 0<=x<=1 and 0<=y<=1 Use approximation pi/4 = area Q / area S. Hint: Do the simulation 10000 times and then 100000. (meaning choose the value of x...

  • 3. In a Monte Carlo method to estimate T, we draw n points uniformly on the unit square [0, 1]2 and count how many poin...

    3. In a Monte Carlo method to estimate T, we draw n points uniformly on the unit square [0, 1]2 and count how many points X fall inside the unit circle. We then multiply this number by 4 and divide by n to find an estimator of T (a) What is the probability distribution of X? b) What is the approximate distribution of 4X/n for large n? (c) For n- 1000, suppose we observed 756 points inside the unit circle....

  • (PYTHON) Use Monte-Carlo method to calculate Pi and get as accurate as Math.pi to 0.000001. Reset...

    (PYTHON) Use Monte-Carlo method to calculate Pi and get as accurate as Math.pi to 0.000001. Reset random number seed at the beginning of your program. Do multiple iterations, for example, n = 1000, 10000.

  • Create a NOTEPAD or PDF file that restates the problem in your own words, specifies what input is needed, w...

    Create a NOTEPAD or PDF file that restates the problem in your own words, specifies what input is needed, what output is expected, the step by step process (algorithm) to get the output from the input, and test data (input for which you know the expected output) for each of the 3 problems given below. You should not write any actual C++ code. Make sure the problem statement is in your own words and is descriptive enough so someone not...

  • Write a PYTHON program that will approximate the value of π. You can do this by...

    Write a PYTHON program that will approximate the value of π. You can do this by computing π to be the ratio of the area of a circle to the area of the square that bounds that circle. Assume a circle of radius 0.5 enclosed by a 1x1 square. The area of the circle then is πr^2=π/4, since r=0.5=1/2 and the area of the square is 1. To approximate the ratio, take a large number of uniformly distributed random points....

  • This problem deals with continuous (rather than discrete) probability, but it's an interesting problem! It involves...

    This problem deals with continuous (rather than discrete) probability, but it's an interesting problem! It involves probabilistically estimating the value of pi. Consider a circle of radius 1 inscribed within a square with side 2. Both shapes are centered at the origin (0, 0). Using basic geometry, the ratio of the circle's area to the square's area is pi (1)^2/2^2 = pi/4. Now, suppose that you randomly throw some darts at this figure. Out of n total attempts, m attempts...

  • [20 points] Problem 2 - Monte Carlo Estimation of Definite Integrals One really cool application of...

    [20 points] Problem 2 - Monte Carlo Estimation of Definite Integrals One really cool application of random variables is using them to approximate integrals/area under a curve. This method of approximating integrals is used frequently in computational science to approximate really difficult integrals that we never want to do by hand. In this exercise you'll figure out how we can do this in practice and test your method on a relatively simple integral. Part A. Let X be a random...

  • last 2pictures are lab7,just need to do the first picture,dont do the lab 7 Functional π...

    last 2pictures are lab7,just need to do the first picture,dont do the lab 7 Functional π Take the sample solutions for approximating π from labZ and put each solution into its own function. You'll need to decide: 1. What are the inputs that this function requires? 2. What is the output that this function produces? 3. What is an appropriate name for this function? You should create 3 separate functions 1, One that approximates π using the area of a...

  • Assignment Specifications We are going to use the Monte Carlo Method to determine the probability of...

    Assignment Specifications We are going to use the Monte Carlo Method to determine the probability of scoring outcomes of a single turn in a game called Pig. Your Task For this assignment you will simulate a given number of hold?at?N turns of a game called Pig, and report the estimated probabilities of the possible scoring outcomes. You are NOT implementing the game of Pig, only a single turn of this game. The value of N will be acquired via user...

  • need help with matLab Question 1 (20 Points) Write a well-documented MATLAB script hmwk7Q1.m that simulates...

    need help with matLab Question 1 (20 Points) Write a well-documented MATLAB script hmwk7Q1.m that simulates tossing 100 coins into a unit square. As shown in the scatter plot. Location of Simulated Coins In Unit Square 1 o0 Ooo 05 04 03 02 oo 0.1 2 03 4 05 07 1 xpostion Hmwk7Q1.fig Consider organizing your MATLAB script into the following sections. % housekeeping (performs clearing of figures, workspace, and command lines) % Initialize the Number of Coins To Simulate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT