# Question: i don know why has been error the monte carlo...

###### Question details

I don know why has been error

The Monte Carlo method is used in modeling a wide-range of physical systems at the forefront of scientific research today. Monte Carlo simulations are statistical models based on a series of random numbers. Let's consider the problem of estimating Pi by utilizing the Monte Carlo method.

Suppose you have a circle inscribed in a square (as in the figure). The experiment simply consists of throwing darts on this figure completely at random (meaning that every point on the dartboard has an equal chance of being hit by the dart). How can we use this experiment to estimate Pi? The answer lies in discovering the relationship between the geometry of the figure and the statistical outcome of throwing the darts. Let's first look at the geometry of the figure.

Let's assume the radius of the circle is R, then the Area of the circle = Pi * R^2 and the Area of the square = 4 * R^2. Now if we divide the area of the circle by the area of the square we get Pi / 4.

But, how do we estimate Pi by simulation? In the simulation, you keep throwing darts at random onto the dartboard. All of the darts fall within the square, but not all of them fall within the circle. Here's the key. If you throw darts completely at random, this experiment estimates the ratio of the area of the circle to the area of the square, by counting the number of darts within each area. Our study of the geometry tells us this ratio is Pi/4. So, now we can estimate Pi as

Pi = 4 x (Number of Darts in Circle) / (Number of Darts in Square)

//-------------------------------c++code----------------------

#include <iostream>

#include <cstdlib>

#include <cmath>

using namespace std;

int main() {

srand(333);

int side; // this is the side of the square and is
also our resolution.

int tries; // this is the number of tries.

cout << "Enter the side of the square:" <<
endl;

//Ask the user for the size (integer) of a side of the
square

cin >> side;

//Get the users input using cin

cout << "Enter the number of tries:" <<
endl;

//Ask the user for the number of tries using
cout.

cin >> tries;

//Get the users input using cin.

int inCount = 0; //counter to track number of throws
that fall inside the circle

for (int i = 0; i < tries; ++i) {

//throw a dart using throwDart
method and increment the counter depending on its output.

if (throwDart(side) == true)

inCount++;

}

//Compute and display the estimated value of PI.
Make sure you are not using integer division.

int outCount = tries - inCount; //falls outside the
circle.

double PI = 4 * (static_cast<double> (inCount) / (outCount));

cout << "Estimated PI Value: " << PI << endl;

return 0;

}

// given the coordinates of a point (x,y) computes if the point is
inside or on the circle

// centered at origin with radius r. Returns 'true' if it is inside
or on the circle, 'false' otherwise.

// DO NOT use the sqrt function here. Just compare to r
squared.

bool isInside(double x, double y, double r) {

double circle_diameter = pow(r, 2);

if (pow(x, 2) + pow(y, 2) < circle_diameter) {

return true;

}

return false;

}

// given s, the size of the side of a square that is centered at
the origin,

// chooses a random coordinates inside the square, and calls
isInside function

// to test if the point is inside the circle or not.

bool throwDart(int s) {

int x, y;

// assign x and y to two random integers between -s/2
and s/2

x = (rand() % (s + 1)) - (s / 2);

y = (rand() % (s + 1)) - (s / 2);

//Call the isInside function and return its
output.

//You do not have to cast x & y to doubles, it is
done automatically.

bool isPointInside = isInside(x, y, s / 2);

return isPointInside;

}