Starting from:

$30

Lab 6 Problem Solution

Estimating the value of π using Monte Carlo method:




The idea is to simulate random (x, y) points in a 2D plane with domain as a square of side 1 unit.

We then calculate the ratio of number points that lied inside the circle and total number of generated points:




<p style="text-align: center;"

π = 4*(circle_points/square_points)

</p




1. Define a function which produces random number in interval [0,1]

2. Use this function to perform evaluation of $\pi$ value

- Ask user for number of iterations

- During each iteration

- Generate two coordinates using the function that produces random coordinates

- Use generated coordinates to check if the point is inside the unit circle (Hint: What is the unit circle equation?)

- Calculate π value from the ratio of point that fall within the unit circle and total number of points in the unit square




Use following code as a starting point for this lab, however you may start from scratch if you choose to do so.




```c++

#include <iostream

#include <cstdlib

#include <cmath

using namespace std;




double randcoord(); // prototype




// put your program in the main function

int main() {




return 0;

}

```

More products