Answered step by step
Verified Expert Solution
Question
1 Approved Answer
USE C Language to programing Tasks: 1. Program-1. An interesting way of calculating n is to use a technique known as Monte Carlo, which involves
USE C Language to programing
Tasks: 1. Program-1. An interesting way of calculating n 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 the figure (assume that the radius of this circle is 1; thus we have a square of size 2x2). First, generate a series of 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: 1=4 x (number of points in circle) / (total number of points) Write a multi-threaded version of this algorithm that creates a separate thread (the slave- thread) to generate a number of random points. The slave-thread will count the number of points that occur within the circle (the hit_count) and store that result in the global variable circle_count. When the slave-thread has exited, the parent thread (the master-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 random points, the closer the approximation of T. Note: Program-l contains only 2 threads; a master-thread and its single slave-thread. Below is the code for generating random numbers, as well as the code for determining if the random (x, y) point occurs within the circle /* Generates a double precision random number */ double random_double() { return random() / ((double) RAND_MAX + 1); } /* seed the random number generator */ srandom( (unsigned) time (NULL)); /*generate random numbers between -1.0 and +1.0 (exclusive) */ /* to obtain a random (x, y) point*/ x = random_double() * 2.0 - 1.0; y = random_double()* 2.0 - 1.0; /* is (x, y) point within the circle ? */ if ( sqrt(x*x + y*y)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