/*--------------------------------------------------------------*/ /* Program xrand (Chapter 11) */ /* */ /* This program is written to test the rand () */ /* random number generator (See Chapter 11:Monte Carlo). */ /* */ #include #include main() { FILE *out; int i; double rand_float(double a, double b); double random, random_new; unsigned int seed=1; srand(seed); out=fopen("output_rand.dat","w"); random=rand_float(0.0,1.0); for (i=1; i<=5000000000; i++) { random_new=rand_float(0.0,1.0); if ((random <= 0.001) && (random_new <= 0.001)) { fprintf(out, "%e %e \n", random, random_new); } random=random_new; } fclose(out); /* */ /* Now view the plot in output_rand.dat using Matlab. */ /* */ return EXIT_SUCCESS; } /* this function generates a random double value between a and b */ double rand_float(double a, double b) { return ((double) rand()/RAND_MAX)*(b-a)+a; } /*---------------------------------------------------------------*/