#include <stdio.h> #include <stdlib.h> #include <time.h> /* Puzzle B02 -- determine experimentally how often rand() returns RAND_MAX */ const int N = 1000 ; int main(int argc, char *argv[]) { int i, j; int count = 0; srand( time(NULL) ); for ( i=0; i<N; i++ ) for ( j=0; j<RAND_MAX; j++ ) { if( rand() == RAND_MAX ) count++ ; } printf("RAND_MAX occured %d times in %d x RAND_MAX trials\n", count, N ); printf("\n"); system("pause"); return 0; }
Comments: Of course, you can easily modify the program so that it counts how often any integer occurs. For example, the integer 100 should also occur N times. You might also wish to test how often RAND_MAX+1 occurs. That should be zero, but perhaps you should test it.