Please use c language 4.22 An interesting way of calculating _ is to use a techn
ID: 3721747 • Letter: P
Question
Please use c language
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
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 _ by performing the following calculation:
_ = 4× (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 _. 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 estimating
_ should consult the bibliography at the end of this chapter. In
Chapter 5, we modify this exercise using relevant material from that
chapter.
1, 1 (0, 0) Figure 4.18 Monte Carlo technique for calculating pi.Explanation / Answer
The below-written program will generate a series of random points based on user input.
These points will be (x,y) coordinates that fall in the Cartesian coordinates in a square (which has a circle inscribed in it).
Based on how many lands in the circle it generates pi.
The program immediately responds with the result of its approximation of pi after it is done calculating.
----- C Program -----
/* Program to compute Pi using Monte Carlo methods */
/* Program to compute Pi using Monte Carlo methods */
#include <stdio.h> #include <string.h> #include <pthread.h> #include <math.h> #include <stdlib.h> int i = 0; int amtWanted = 0; int totalPts = 0; void *count(void *X) { /* initialize random numbers */ for (i=0; i < amtWanted; i++) { double X = (double)rand() / RAND_MAX; double Y = (double)rand() / RAND_MAX; if (((X * X) + (Y * Y)) <= 1) { totalPts++; // increment number of points inside circle } } return NULL; } int main() { printf(" Welcome to Threaded Monte Carlo "); srand(time(NULL)); pthread_t thread; do { printf("Please enter a positive number for the number of points you would like to generate? "); scanf("%d", &amtWanted); }while (amtWanted <= 0); pthread_create(&thread, NULL, &count, NULL); pthread_join(thread, NULL); double points = 4.0 * totalPts; double pi = points / amtWanted; printf("The approximate value of pi for the desired amount of points (%d) is: %f ", amtWanted, pi); return 0; }