develop an algorithm to estimate PI using basic geometry and random number gener
ID: 3562937 • Letter: D
Question
develop an algorithm to estimate PI using basic geometry and random number generation.
We have a circumscribed circle of radius 1 inside a square area with sides of length 2. By randomly generated points (x,y) within the square we can use the equation from high school geometry to determine the distance from the center of the circle to any randomly generated point.1
(0,0)
(2,2)
(2,0)
(0,2)
r = 1
(1,1)
As a reminder we utilize the Pythagorean Theorem to determine the distance between two points:
Distance = sqrt( (xr - x0)2 + (yr - y0)2 )
We can then use this formula to determine if a randomly generated point (xr , y r ) on the intervals: 0 < x r < 2, 0 < y r < 2 lies within the circumscribed circle.
If we generate enough of these points we can determine a ratio of points within the circle to total points generated. Knowing the area of the square to be simply 4 in this case, the fractio of points in the circle to the total number of points will provide us with the area of the circle from which PI can easily be determined
Explanation / Answer
Start:
int x[]=new int[3];
int y[]=new int[3];
int a[]={0,1,2};
double distance;
Random ran=new Random();
for(int i=0; i<a.size;i++)
{
x[i] = ran.nextInt(a.size);
y[i] = ran.nextInt(a.size);
}
Double radius=1;
Double area_of_ square=4; //since all the sides are of length 2
Double area_of_cirlce=1/3; //since the circle resides inside the square, consider one point of the circle that intersects the square that is 1 point and total points generated are 3.
Print “the area of circle is (PI*r*r):” ;
Double PI=area_of_ circle/(r*r);
Print “the PI value is ” PI;
Stop
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.