Approximate an integral using Monte Carlo method

I have a question on an assignment

Calculate the value of the integral I = $\int_0^\pi sin^2(x)dx$ using the Monte Carlo Method (by generating $10^4$ uniform random numbers within domain [0, π] × [0, 1]). First do the calculation using LCG (Linear Congruent Generator) with a = $7^5$ , c = 0 and m = $2^{31}$ − 1, then by Matlab’s build-in generator.

As I understand, I have to generate $10^4$ uniform random numbers using the LCG algorithm with given parameters. But how I make sure they are within [0, π] × [0, 1] domain? Actually, what exactly "[0, π] × [0, 1] domain" means? LCG gives just random numbers between 0 and 1.

As I understand, I need to do something like this in C

double x = 1; // initial random number
// i is between 0 and Pi
for (double i = 0; i <= M_PI; i += M_PI/10000) {
// generate the random number [0, 1] using LCG with the given params
x = ((16807*x + 0) % 2147483647)/2147483647;
if (sin(i)*sin(i) > x) {
miss = miss + 1;
}
total = total + 1;
}
// calculate area based on the ratio of total vs. miss


why are we comparing $sin^2$ with a number between 0 and 1?

is it the other way around?

double x = 1; // initial random number
// i is between 0 and 10^4
for (double i = 0; i < 10000; i ++) {
// generate the random number [0, Pi] using LCG with the given params
x = ((16807*x + 0) % 2147483647)/2147483647 * M_PI;
if (sin(i)*sin(i) > x) {
miss = miss + 1;
}
total = total + 1;
}
// calculate area based on the ratio of total vs. miss


then why do we pass into $sin$ something that is between 0 and 10000?

I'm confused with how to apply the Monte Carlo method.

You need to generate $2\times 10^4$ values, $10^4$ for $x\in [0,\pi]$ and $10^4$ for $y\in[0,1]$ and combine them as the coordinates of the points. Then test whether $y\leq \sin^2{x}$.
Comparing the area within the region between $[0,1]$ is to give it a bound. You can of course do it within $[0,2]$. However you choose the bound, you will need to multiply the ratio you get by the area of the whole region. If you choose $[0,1]$, you multiply the ratio by $\pi$. If you choose $[0,2]$, you multiply the ratio by $2\pi$.
• You cannot use the $r$ to divide by $m$ since you need to use the original $r$ later. You have to define another variable $u=r/m$ in each step. Mar 23, 2015 at 15:14