Horribly confused by the rand operator and this Monte Carlo problem

I am horribly confused by how I'm supposed to get those outputs listed below. My current code is also below the instructions. How do I set up the 2 x 2 square within the rand operator? I'm also completely stuck on how to even find the areas with the random darts.

Problem 1: (Monte Carlo Experiments)
The Monte Carlo method is used in modeling a wide-range of physical systems at the forefront
of scientific research today. Let’s consider the problem of estimating the area of the region
x^2 + 2y^2 ≤ 1 by utilizing the Monte Carlo method. This region is enclosed by a blue ellipse,
which is inscribed in a 2 × 2 red square (shown in the figure).
The experiment simply consists of throwing darts on this figure completely at random (meaning that every point in the red square has an equal chance of being hit by the dart). You
keep throwing darts at random. All of the darts fall within the square, but not all of them
fall within the ellipse. If you throw darts completely at random, this experiment estimates
the ratio of the area of the ellipse to the area of the square, by counting the number of darts
within each area.
Program this Monte Carlo method to compute the area of the blue ellipse using different
numbers of darts. For each experiment, use N darts where N = 10, 10^2
, · · · , 10^6
, and output
the estimated area and the corresponding numerical error |estimated area−exact area|. The
exact area for the ellipse is given by
sqrt(2)/2 times pi ≈ 2.22144146907918.
A successful run of your code may look like:
# darts: 10 estimated area = 2.40000 error = 0.17856
# darts: 100 estimated area = 2.16000 error = 0.06144
# darts: 1000 estimated area = 2.18000 error = 0.04144
# darts: 10000 estimated area = 2.23680 error = 0.01536
# darts: 100000 estimated area = 2.21072 error = 0.01072
# darts: 1000000 estimated area = 2.22037 error = 0.00107

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;

double Area_Ellipse(double c) {
double Area_square = 4 * c * c;
double area;
double N;
double totaldarts;

cout << "# darts: ";
cin >> N;
int count = 0;
for (int k = 1; k < N; k++) {
totaldarts = pow(10, k);
for (int i = 1; i <= totaldarts; i++) {
double x = -c + 2 * c * 1.0 * rand() / RAND_MAX;
double y = -c + 2 * c * 1.0 * rand() / RAND_MAX;
if ((x*x) + 2 * (y*y) <= 1) {
count++;
}
}

}
area = (4 * count) / totaldarts;
return area;


}

int main()
{
double a, b;
//cout << "Please input a and b (<=5): " << endl;
//cin >> a >> b;
double c = 5.0;
double count;
double Area = Area_Ellipse(c);

cout << Area << endl;

}
@Allosaurus

First of all, please PUT YOUR CODE IN CODE TAGS. Look at your post: it is almost unintelligible. You will get many more replies if you make an effort to format your posts in a manner that allows them to be read easily. If you put your code in code tags: (a) we can see the indentation, and hence structure; (b) we can run it directly in the online compiler without having to go through the fuss of copying it.

Please try to explain to someone why you have set c = 5.0. You are specifically asked in the question to use a square of side 2, extending, it would seem, from -1 to 1. Thus, if c seems to be left as a free variable (there is nothing in the question implying that necessity) then c = 1.0

If you ask for the number of darts (N) ... then please don't try to generate 10N of them! It could take a very long time. TBH it would be simpler just to have N as a single variable input by the user and pass N as an argument to Area_Ellipse, not c. You do NOT need the loop starting
1
2
for (int k = 1; k < N; k++) {
totaldarts = pow(10, k);


For what is actually given in the question, the following will do (and corresponds to c=1):
double x = -1 + 2.0 * rand() / RAND_MAX;
double y = -1 + 2.0 * rand() / RAND_MAX;
The condition that it lies within the ellipse is OK, if somewhat over-bracketed.

You haven't included a call to srand(), so you will get (for a given N) the same answer each time. That's not random.

The total area of a 2x2 square is 4 units, so if you calculate the fraction that lie in the ellipse (successes/throws, taking care over integer division) then the area of the ellipse will be fraction x (area of square).

Basically, your area routine should deal with one N value only. You should also start small and then build up: make your code work for a single value of N, and later modify it to run for more (if you actually deem that necessary).

Reformat your code taking account of the above, put it in code tags, and re-post BELOW.
Last edited on
Topic archived. No new replies allowed.