Question: using c++ Monte-Carlo is a method that minics the processe of random sampling to obtain numerical results. For example, consider a rectangle of size 2
using c++
Monte-Carlo is a method that minics the processe of random sampling to obtain numerical results. For example, consider a rectangle of size 2 x 2 which contains an incircle of radius 1. Putting this rectangle in an open area when it is raining, the amount of rain-drops collected in the rectangle are propotional to the surface of the rectangle, the amount of rain-drops collected in the incircle is propotional to the surface area of the incircle. That is: rain_in_rectangle / rain_in_incircle = 2*2/pi
We can mimic the rain-drops as random points in Cartesian coordinates with (x,y). By fixing -1<= x <=1 and -1<=y<=1, the condition x*x+y *y <=1 will give you the rain-drops within the incircle( See the figure). Then the value of PI can be approximated by counting the total of occurances of of points within the incircle (N_incircle) and the total number of points you generate ( N ) as ( 4.0* N_incircle /N )
Use these information and the hints provided below, design a program in C++ that approximates the value of PI.
Hints:
1) define float type variable x, y, integer variable i, N,N_incircle;
2) intialize N to be a big number which is the total rnumber of random points
3) design a loop using index i, generate a random point (x,y)
x =2.0* (float) rand()/RAND_MAX -1.0;
y=2.0* (float) rand()/RAND_MAX -1.0;
4) check this point is in circle
is x*x+y*y <=1.0? Yes==> N_incircle +=1;
5) Repeat step 3-4) until i =N
6) approximate PI as: 4.0*N_incircle /N
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
