Homework 9: Customized Random Functions
Due: Tuesday 4/10 by 11:59pm
Objectives
More practice implementing functions, plus practice working with global variables and separate compilation.
Background
The standard C library provides a function random() that returns a pseudo-random integer. This function is somewhat difficult to use because we often prefer to have a random number within a certain range of values. For example, to obtain a random value between 1 and 10 (inclusive) we have to use something like:
n = random() % 10 + 1 ;
Similarly, to obtain a value between 15 and 25 inclusive, we have use:
n = random() % 11 + 15 ;
Notice that in this second case, we need to mod out by 11 (not by 10) because there are 11 values between 15 and 25. The expression random() % 11 gives us a value between 0 and 10. When we add this to 15, we get a random value between 15 and 25.
Before we can use random() we have to "set" the random seed. (Otherwise, we would get the same sequence of "random" numbers each time.) A typical way of doing this is to use the clock and make a call to srandom() at the beginning of a program:
srandom(time(0)) ;
The time() function returns the number of seconds since 00:00:00 UTC, January 1, 1970. So, each run of the program (started more than 1 second apart) will set then random seed to a different value.
Here is an example program that uses the random functions from the standard C library to print out 100 numbers between 15 and 25: hw09.c. Notice in the sample run that we get a different sequence of random numbers in the 3 runs. Try commenting out the call to srandom(). Then you get the same numbers every time you run the program.
Assignment
Your assignment is to provide two random functions mysrandom() and myrandom() that are easier to use than the ones provided by the C library. A programmer using your functions will call your functions and your functions will in turn call srandom() and random() from the standard library. Such functions are commonly called "wrappers".
For example, we have here a main program that uses mysrandom() and myrandom():
1 /* File: hw09main.c 2 3 Main program for Homework 9. 4 Do not change this file. 5 6 */ 7 8 #include <stdio.h> 9 10 // include student's header file: 11 12 #include "myrandom.h" 13 14 int main() { 15 int i ; 16 17 // initialize random number generator and 18 // set range from 1 to 10. 19 20 mysrandom(1, 10) ; 21 22 printf("Random number #1 = %d\n", myrandom() ) ; 23 printf("Random number #2 = %d\n", myrandom() ) ; 24 printf("Random number #3 = %d\n", myrandom() ) ; 25 26 // set range from 10 to 20. 27 mysrandom(10, 20) ; 28 29 printf("\n") ; 30 printf("One hundred random numbers between 10 and 20 (inclusive):\n") ; 31 for (i = 1 ; i <= 100 ; i++) { 32 printf("%5d", myrandom() ) ; 33 34 if (i % 10 == 0) printf("\n") ; 35 } 36 37 // set range from 37 to 53. 38 mysrandom(37, 53) ; 39 40 printf("\n") ; 41 printf("Two hundred random numbers between 37 and 53 (inclusive):\n") ; 42 for (i = 1 ; i <= 200 ; i++) { 43 printf("%5d", myrandom() ) ; 44 45 if (i % 10 == 0) printf("\n") ; 46 } 47 48 49 return 0 ; 50 } 51
(You can download the main program without line numbers here: hw09main.c.) Notice that mysrandom() takes two parameters. For example in line 20, mysrandom(1,10) is intended to say that future calls to myrandom() should return a random value between 1 and 10. Lines 27 and 38 have additional calls to mysrandom().
The intended output of this program is:
PT[52]% ./a.out Random number #1 = 9 Random number #2 = 8 Random number #3 = 5 One hundred random numbers between 10 and 20 (inclusive): 18 14 15 20 18 20 16 10 16 16 20 11 13 13 10 16 15 11 13 20 19 17 16 17 12 20 12 16 19 17 16 14 19 11 11 16 19 18 14 12 13 13 11 16 15 11 20 20 13 12 19 20 17 15 16 20 12 17 13 10 11 20 13 20 19 14 13 15 11 17 15 12 19 17 17 13 16 16 12 17 17 11 14 11 14 19 10 16 15 12 14 16 20 15 13 16 20 16 19 19 Two hundred random numbers between 37 and 53 (inclusive): 45 47 51 46 51 40 44 46 46 52 49 52 46 48 47 52 50 51 38 42 50 49 39 41 45 37 49 38 39 48 50 38 49 48 38 46 42 46 46 42 44 41 48 53 44 41 42 40 39 43 45 43 46 48 47 38 39 51 47 41 53 44 51 48 46 52 48 42 44 40 38 42 53 50 50 43 45 38 46 38 53 38 52 53 40 37 37 42 51 39 37 50 37 42 52 37 41 46 50 39 49 51 45 48 38 41 37 47 50 38 39 49 47 38 49 50 38 40 46 43 50 37 39 50 43 45 50 38 37 46 40 49 52 48 52 53 52 43 37 49 44 40 52 37 41 38 41 50 50 41 39 46 42 49 43 39 40 47 40 40 48 43 43 46 46 41 45 52 39 46 38 46 49 45 47 44 46 42 40 50 47 42 51 52 37 48 45 48 41 48 51 52 45 48 44 45 44 44 44 46 PT[53]%
Your assignment has two parts.
- You must create a header file called myrandom.h that has the function prototypes for mysrandom() and myrandom(). These prototypes must be compatible with the main program in hw09main.c.
- You must implement the functions mysrandom() and myrandom(). These functions must provide the features described above and work with the main program without alteration. These implementations go in a separate file called myrandom.c.
Notes
- Don't panic. There's a lot of verbiage for this homework description, but you don't actually have to write a lot of code (much less than usual actually).
- Read all of these notes. They are long, but they tell you what you need to do.
- Make sure you understand how the regular srandom() and random() functions work. Read the Background information above and play with the example program: hw09ex.c.
- Make sure you understand header files and separate compilation. You may need to review Classwork 12 and the textbook.
- Each call to myrandom() has to know what the programmer wants for the range of random numbers. This must be stored in two global variables. When mysrandom() is called, the low end and high end of the range are stored in these global variables. When myrandom() is called, the values of the low end and high end are retrieved from the global variables. Global variables are declared just like local variables, except the declaration goes outside all functions, usually just below where you have the #include directives.
- If you are having trouble with global variables, take a look at global.c.
- You do not have to #include "myrandom.h" in myrandom.c but it is a good way to make sure that your functions in myrandom.c are compatible with the function prototypes in myrandom.h.
- Yes, you have to figure out the function prototypes for mysrandom() and myrandom(). You should look at the main program to see what the prototypes should be. There is enough information in the way these functions are used in main() for you to figure this out. This is part of your assignment.
- Yes, you can call the standard random()
function from myrandom(). (This makes the assignment
easier). You need to have
#include <stdlib.h>
in your myrandom.c file.
- When you implement myrandom(), look carefully at the example above that produces a random number between 15 and 25. You need to be able to do something similar but for any value for the low end and high end.
- Your implementation of mysrandom()
should call the standard srandom() to set
the random seed. (Otherwise, you get the same
sequence of "random" numbers every time — that's
not very random.) You should call srandom()
with the time() function as described previously:
srandom(time(0)) ;
In order to use time(), you have to
#include <time.h>
Submitting
Use the script command to record yourself compiling the main program and your implementation of mysrandom() and myrandom() separately:
gcc -c -Wall hw09main.c gcc -c -Wall myrandom.c gcc hw09main.o myrandom.o
Submit your C program, header file and the typescript file as usual:
submit cs104_chang hw09 myrandom.h myrandom.c typescript
You should not submit the main program because your functions should work with the main program unaltered and must compile with the original version.