# finding area of unit circle by monte carlo..

• 02-24-2008
niceguy
finding area of unit circle by monte carlo..
Hi guys, I am attempting this, but cannot put finger on what is wrong. I have done it assuming a unit circle on origin and I thus use the positive quadrant and multiply the answer by 4.

Think maybe to do with the way I have generated random numbers.

insert
Code:

``` #include<stdlib.h>  #include<stdio.h>  #include<math.h>  int main() {     int N=100000,j; double x1,x2,hit;     x1=rand()/(double)RAND_MAX; //insuring random numbers between 0,1   x2=rand()/(double)RAND_MAX;     for(j=0;j<N;j++)          if(x1*x1+x2*x2<=1)                 {              hit++;           }         printf("volume: &#37;f", (4.0*hit)/N);          system("pause"); exit(0); }```
• 02-24-2008
arpsmack
You aren't generating the random numbers in your loop...
Oh you should probably initialize hit too...
• 02-24-2008
niceguy
Quote:

Originally Posted by arpsmack
You aren't generating the random numbers in your loop...
Oh you should probably initialize hit too...

Ok i think i made the modifications...but still not right.

include
Code:

``` #include<stdio.h> #include<stdlib.h>  #include<math.h>  #include<time.h>  int main() {     int N=100000,j; double x1,x2,hit=0;   srand(time(NULL));     x1=rand()/(double)RAND_MAX;   x2=rand()/(double)RAND_MAX;     for(j=0;j<N;j++)          if(x1*x1+x2*x2<=1)                 {              hit++;           }         printf("volume: &#37;f\n", (4.0*hit)/N);      system("pause"); exit(0); }```
• 02-25-2008
vart
Code:

```for(j=0;j<N;j++)  {   x1=rand()/(double)RAND_MAX;   x2=rand()/(double)RAND_MAX;   if(x1*x1+x2*x2<=1)   {              hit++;   } }```
• 02-25-2008
slingerland3g
With Vart's code you may want to change your equation to:

printf("Area of circle: &#37;f\n", 4.0 * (hit/N));

Guessing you are needing area and not volume? Area = r^2 *PI