The following code correct(er):
Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define NUM 100
#define IMAX 10
#define ISEED 777861
#define ISEED1 777862
#define RND() ((double)rand()/(double)RAND_MAX)
int main ()
{
int i, naccept=0, nreject=0, ntrial=0, c;
double x[NUM],s[NUM],r[NUM],pi;
srand( time(NULL) );
naccept=0; nreject=0; ntrial=0;
for (i=0; i<NUM; i++)
{
x[i]=RND();
}
int j;
double y[NUM];
//srandom(ISEED1);
for (j=0; j<NUM; j++)
{
y[j]=RND();
}
for (c=0; c<NUM; c++)
{
r[c]=x[c]*x[c]+y[c]*y[c];
printf("r=%f\n",r[c]);
s[c]=r[c]*r[c];
printf("s[c]=%f\n",s[c]);
if (s[c]<1.0){
naccept++;
}
else {
nreject++;
}
}
ntrial= naccept+nreject;
printf("naccept=%d nreject=%d ntrail=%d\n",naccept,nreject,ntrial);
pi=4.0*((double)naccept/(double)ntrial);
printf("pi=%.3f\n",pi);
}
I suggest you update the code because are sure using some non-standard stuff.
It almost produces correct pi (ca 3.04x something I believe).
And only initialize the seed once.