/* This program is copyright (c) 2000 by D E Knuth; * you may copy it freely AS LONG AS YOU MAKE ABSOLUTELY NO CHANGES! * You could also change it, but then you must rename the file and * tell people clearly that your version is not the same as mine. * It is explained in Seminumerical Algorithms, 3rd edition, Section 3.6 * (or in the errata to the 2nd edition --- see * http://www-cs-faculty.stanford.edu/~knuth/taocp.html * in the changes to pages 171 and following of Volume 2). */ /* If you find any bugs, please report them immediately to * taocp@cs.stanford.edu * (and you will be rewarded if the bug is genuine). Thanks! */ /************ see the book for explanations and caveats! *******************/ /************ in particular, you need two's complement arithmetic **********/ /* old-style C function declarations appear here for reasons of portability */ #define KK 100 /* the long lag */ #define LL 37 /* the short lag */ #define mod_sum(x,y) (((x)+(y))-(int)((x)+(y))) /* (x+y) mod 1.0 */ double ran_u[KK]; /* the generator state */ /* void ranf_array(double aa[], int n) */ void ranf_array(aa,n) /* put n new random fractions in aa */ double *aa; /* destination */ int n; /* array length (must be at least KK) */ { register int i,j; for (j=0;j=1.0) ss-=1.0-2*ulp; /* cyclic shift of 51 bits */ } for (;j0;j--) ul[j+j]=ul[j],u[j+j]=u[j]; /* "square" */ for (j=KK+KK-2;j>KK-LL;j-=2) ul[KK+KK-1-j]=0.0,u[KK+KK-1-j]=u[j]-ul[j]; for (j=KK+KK-2;j>=KK;j--) if(ul[j]) { ul[j-(KK-LL)]=ulp-ul[j-(KK-LL)], u[j-(KK-LL)]=mod_sum(u[j-(KK-LL)],u[j]); ul[j-KK]=ulp-ul[j-KK],u[j-KK]=mod_sum(u[j-KK],u[j]); } if (is_odd(s)) { /* "multiply by z" */ for (j=KK;j>0;j--) ul[j]=ul[j-1],u[j]=u[j-1]; ul[0]=ul[KK],u[0]=u[KK]; /* shift the buffer cyclically */ if (ul[KK]) ul[LL]=ulp-ul[LL],u[LL]=mod_sum(u[LL],u[KK]); } if (s) s>>=1; else t--; } for (j=0;j=0? *ranf_arr_ptr++: ranf_arr_cycle()) double ranf_arr_cycle() { ranf_array(ranf_arr_buf,QUALITY); ranf_arr_buf[100]=-1; ranf_arr_ptr=ranf_arr_buf+1; return ranf_arr_buf[0]; } int main() { register int m; double a[2009]; /* a rudimentary test */ ranf_start(310952); for (m=0;m<2009;m++) ranf_array(a,1009); printf("%.20f\n", ran_u[0]); /* 0.27452626307394156768 */ ranf_start(310952); for (m=0;m<1009;m++) ranf_array(a,2009); printf("%.20f\n", ran_u[0]); /* 0.27452626307394156768 */ return 0; }