/* Monte carlo random dot method */
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define a 1
#define b 2
#define cycle 5000
int main()
{
int counter=0, r1, r2, ymax, i, y;
float f, A;
ymax=3*2*2;
for(i=0;i<cycle;i++)
{
r1=a+rand()%ymax;
y=3*r1*r2;
r2=rand()%ymax;
if (y>=r2)
counter++;
}
f=(float)counter/(float)cycle;
A=f*(float)ymax*((float)b-(float)a);
printf("The required integration is %f", A);
return(0);
}
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define a 1
#define b 2
#define cycle 5000
int main()
{
int counter=0, r1, r2, ymax, i, y;
float f, A;
ymax=3*2*2;
for(i=0;i<cycle;i++)
{
r1=a+rand()%ymax;
y=3*r1*r2;
r2=rand()%ymax;
if (y>=r2)
counter++;
}
f=(float)counter/(float)cycle;
A=f*(float)ymax*((float)b-(float)a);
printf("The required integration is %f", A);
return(0);
}
Comments
Post a Comment