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

Need help in this programming, need someone to really help me out, I am not a pr

ID: 3638854 • Letter: N

Question

 

Need help in this programming, need someone to really help me out, I am not a programmer!!  anywhere that says p its pie

 

p can be estimated by choosing random points and determining the fraction of the points which are within the upper right quarter of a unit circle. This is an example of the Monte Carlo Method, or a Monte Carlo Simulation.

Write a Java program that estimates the value of p using a Monte Carlo simulation. Your program must make and display ten estimates of the value of p. After all ten estimates are displayed, the program must display the number of iterations required for each estimate. By iteration, I mean the choice of one random point and determining whether the point is within the region (a circle, in this case).

Each estimate must be within 0.000001 of the value of Math.PI, meaning that, for each of the ten estimates, the program must iterate until (Math.abs(Math.PI - estimate) < 1.0e-6) is true, where 1.0e-6 is a convenient way to express 1.0 × 10-6 in Java.

Thus your program will have an outer for loop that executes 10 iterations, each time calculating an estimate for p, displaying the estimate, and storing the number of iterations of an inner while loop in an array.

Within the for loop, there will be a while loop, that repeatedly picks points and checks each to determine whether it is within the circle. Once the estimate is within the tolerance given above, display the estimate and store the number of iterations needed to get that estimate in an array.

After the for loop terminates, display all ten numbers of iterations stored in the array. As an example, one of the times I ran my program, the output looked like this:

Estimate: 3.1415929203539825
Estimate: 3.1415929203539825
Estimate: 3.141593409631227
Estimate: 3.1415918273726557
Estimate: 3.1415929203539825
Estimate: 3.1415932214924966
Estimate: 3.1415916629029983
Estimate: 3.1415918005820576
Estimate: 3.141593348527483
Estimate: 3.1415931784814926
5424
4068
36174
56677
14012
58774
3863695
7903
392695
514255

Explanation / Answer

The idea itself is very simple: Take a square board and draw a bullseye such that it takes up as much of the square as possible. Using an unlimited number of darts and by keep on throwing the darts at the square without focusing on a specific target (just at the square as a whole).


It turns out, statistically, that if we divide the [area of that bullseye] by [the area of the square] we get pi/4 (or a fourth of that square). Multiplying both sides by 4, we get pi = 4 * [area of the bullseye] / [area of the square].


 If you think of the darts as pixels on the screen: Pi = 4 * [number of pixels inside the circle] / [number of pixels in the square].


Soooo ... let's do some Java!

Output:



Code:

 

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote