Please complete in Java An interesting way of calculating (pi) is to use a techn
ID: 3705794 • Letter: P
Question
Please complete in Java
An interesting way of calculating (pi) is to use a technique known as Monte Carlo, which involves randomization. This technique works as follows: Suppose you have a circle inscribed within a square, as shown in Figure 4.18. (Assume that the radius of this circle is 1.) First, generate a series of random points as simple (x, y) coordinates. These points must fall within the Cartesian coordinates that bound the square. Of the total number of random points that are generated, some will occur within the circle. Next, estimate pi by performing the following calculation:
pi = 4x (number of points in circle) / (total number of points)
Write a multithreaded version of this algorithm in Java that creates a separate thread to generate a number of random points. The thread will count the number of points that occur within the circle and store that result in a global variable. When this thread has exited, the parent thread will calculate and output the estimated value of pi. It is worth experimenting with the number of random points generated. As a general rule, the greater the number of points, the closer the approximation to pi. In the source-code download for this text, we provide a sample program that provides a technique for generating random numbers, as well as determining if the random (x, y) point occurs within the circle. Readers interested in the details of the Monte Carlo method for estimating pi should consult the bibliography at the end of this chapter. In Chapter 5, we modify this exercise using relevant material from that chapter.
4.22 An interesting way of calculating is to use a technique known as Monte Carlo, which involves randomization. This technique works as follows: Suppose you have a circle inscribed within a square, as shown in Figure 196 Chapter 4 Threads Figure 4.18 Monte Carlo technique for calculating pi. 4.18. (Assume that the radius of this circle is 1.) First, generate a series of random points as simple (x, y) coordinates. These points must fall within Of the total number of the Cartesian coordinates that bound the s random points that are generated, some will occur within the circle Next, estimate ? by performing the following calculation: IT4x (number of points in circle) / (total number of points) Write a multithreaded version of this algorithm that creates a separate thread to generate a number of random points. The thread will count the number of points that occur within the circle and store that result in a global variable. When this thread has exited, the parent thread will calculate and output the estimated value of T. It is worth experimenting with the number of random points generated. As a general rule, the greater the number of points, the closer the approximation to ? In the source-code download for this text, we provide a sample program that provides a technique for generating random numbers, as well as determining if the random (x, y) point occurs within the circle Readers interested in the details of the Monte Carlo method for esti- mating T should consult the bibliography at the end of this chapter. In Chapter 5, we modify this exercise using relevant material from that chapterExplanation / Answer
CODE:
#include<omp.h>
#include<stdio.h>
#define PI 3.14159
main()
{
int intervals,i;
float sum,x,tsum,h,psum,sumth;
printf("enter the intervals required for calculation");
scanf("%d",intervals);
if(intervals<=0)
{
exit(1);
}
sum=0.0;
h=1/intervals;
for(i=0;i<intervals+1;i=i+1)
{
x=h*(i-0.5);
sumth=sumth+4.0/1+x*x;
}
psum=sumth*h;
sum=sum+psum;
printf("the value of pi is %t %1.16f,sum,fabs(sum-PI));
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.