Question
The objective of this Program is to gain experience with the random number generator by writing a program to determine as estimate for the value
The objective of this Program is to gain experience with the random number generator by writing a program to determine as estimate for the value of the irrational number pi.
The value of pi, often represented by the Greek letter ??, cannot be represented exactly as it is an irrational number. To 8 decimal places, ?? = 3.14159265 .
The value of ?? can be found by several methods. One method is to use a Monte Carlo simulation involving a random number generator.
Consider a circle of radius r inscribed inside a square with side length 2r.
The area of a quarter of the circle is ????4 = 14 ???? The area of a quarter of the square is ????4 = 14 ????
?? 1???? ?? Theratiooftheseareasis ??4 = 4 = ?? ????4 14???? ????
Note the two ratios give exactly the same value, so you can use the full square, or a quarter of the square and the ratios will yield the same value.
We cant calculate circular areas without knowing the value of ??. But, we can get an estimate of the areas by randomly locating many points in the region. If we form the ratio of the numbers of points, we can get an estimate for ??. Think of playing a game of darts with a circular dart board mounted on a square frame. Most of the time the darts hit the board inside the circle, but sometimes they hit outside the board, but inside the frame. (Sometimes they hit the frame, or the wall around it, but were not going to worry about those cases!)
If N points are selected at random to always be inside the square, most points will also be inside the circle. For a large enough sample size, the ratio of the number of points in the circle,
???? , to the number of points in the square, ???? , should be a good approximation of the ratio of the areas: ???? ? ???? 4?? ???? ????
and this ration can be used to determine pi: ?? = ?? ?? . ??
You are to write a C++ program to find an estimate for the value of ??.
EGR 126
Use a technique of randomly locating points in the square region. Every point you find should be inside the square region; some (actually, many) of the points will also be inside the circular part as well. Keep a count of how many points have been generated that is the count of the number of point inside the square. For each point, determine if it is also inside the circular region, and keep a count of how many points are also inside the circular region. Form the ratio of the two counts. We already know the true value of pi, so we are going to compare the calculated (ratio) value of pi, to the true value, and keep adding points to the region until the calculated value is close enough.
The program is easiest if you use the quarter circle/square model the one outlined in red. By imposing an x-y coordinate system on the picture, you can see that the square in the red highlighted area is
drawnfor0??? ??? and 0??? ??? .
Design a function to generate a random value that falls in the interval [0, ??]. Call the function once to generate a value for x, and then call the same function again to generate a value for y. These two values determine a point (x, y) within the xy coordinate system centered at the origin (0,0), and located
somewhere in the quarter region shown in red. This point will always be inside the square of side length r.
Check to see if the point is also inside the circle. A point is inside the circle if ??2 + ??2 ? ??2.
Determine the fewest number of points needed to find your estimate of ?? accurate to 1, 2, 3, 4, 5, and 6 decimal places. For our purposes, you can check against the known value for ??.
Hint: you may want to consider nesting a for loop and a while loop.
Run your program at least two or three times for at least 2 different radius values to see the effect the random number generator has in how many points are needed for the specified accuracies.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started