#include #include unsigned long seed=7; float quickdirtyrand(unsigned long *seed); double parkmillerrand(long *idum); /* Exercise 1.3, see README */ main(int argc, char **argv) { /* This code assumes a mantissa in a double greater than the size of an int. If this is not true on your machine, you may end up in an infinite loop */ int i; double x,y; FILE *fp; seed=7; fp=fopen("quickdirtyxy.dat","w"); for (i=0;i<10000;i++) { x=quickdirtyrand(&seed); y=quickdirtyrand(&seed); fprintf(fp,"%g %g\n",x,y); } fclose(fp); seed=7; fp=fopen("parkmillerxy.dat","w"); for (i=0;i<10000;i++) { x=parkmillerrand(&seed); y=parkmillerrand(&seed); fprintf(fp,"%g %g\n",x,y); } fclose(fp); } float quickdirtyrand(unsigned long *seed) { long ia=106; long ic=1283; long im=6075; *seed = (*seed*ia + ic)%im; return((float) *seed/(float) im); } #define IA 16807 #define IM 2147483647 #define AM (1.0/IM) #define IQ 127773 #define IR 2836 #define MASK 123459876 double parkmillerrand(long *idum) { long k; double ans; *idum ^= MASK; k=(*idum)/IQ; *idum=IA*(*idum-k*IQ)-IR*k; if (*idum < 0) *idum += IM; ans=AM*(*idum); *idum ^= MASK; return ans; }