/* File: rand6.c And, put code in functions. Sixth try. Using time to set the seed. Also, generate numbers between 1 and 6. And, put code in functions. Allow user to set high and low. */ #include #include #include #include "genlib.h" #include "simpio.h" /* Global Variables */ int low_range, high_range ; void SetRandomSeed(void) ; void SetRandomRange(int low, int high) ; int GetRandomNumber(void) ; void SetRandomSeed(void) { int time_seed ; /* Use the time function to set the seed. */ time_seed = (int) time(0) ; srand(time_seed) ; } void SetRandomRange(int low, int high) { low_range = low ; high_range = high ; } int GetRandomNumber(void) { int r ; /* Call rand() to get a large random number. */ r = rand() ; /* Scale the random number within range. */ r = r % (high_range - low_range + 1) + low_range ; return(r) ; } main() { int i, j, go_ahead ; int r1, r2 ; SetRandomRange(1,6) ; while (TRUE) { printf("\nEnter 1 to continue: ") ; go_ahead = GetInteger() ; if (go_ahead != 1) break ; SetRandomSeed() ; /* Print out 50 random numbers in a 10 x 5 grid */ printf("Sum of two dice between 1 and 6\n") ; for (i = 0 ; i < 10 ; i++) { for (j = 0 ; j < 5 ; j++) { r1 = GetRandomNumber() ; r2 = GetRandomNumber() ; printf("%8d ", r1 + r2 ) ; } printf("\n") ; } } }