Sections 0101, 0102, 0103 and Honors, Fall 1995
Thursday October 26, 1995
Assigned Reading: 8.2 - 8.5
Handouts (available on-line): none
Topics Covered:
- Discussed some implementation considerations for Project 3.
See
comments on Project 3.
- We started developing a library for generating
random numbers. Our first program
simply called the function rand() to print out
many random numbers. One problem with this program is that
the output from the program is
the same set of random numbers.
- We consulted the manual to solve this problem. First
we issued the UNIX command:
man -k random
to get a list of functions
related to the word "random". Then, we looked at the
manual pages
for the function rand() by typing:
man rand
- Using the information from the manual pages, we wrote a
second program that sets the
random seed by asking the user for a random number.
Now, the program gives different output
if the user types in a different random seed. However, if
the user types in the same seed, the program still generates
identical output.
- To fix this second problem, we want to use the system clock
to set the random seed. So, we consulted the
manual pages for the time function.
- The third version of our program
uses the time function to set the random seed. This program
prints out a different
list of random numbers every time.
In order to use the time function properly
we needed to include the header file time.h at the
beginning of the program. (We knew this from reading the
manual.)
- We then wrote the fourth version
of the program which prints a list of
random numbers between 1 and 6, so we can simulate the roll
of a six-sided die.
- Next, we broke up our program
into several functions and used defined constants instead of
magic numbers. The output shows
the result of calling the GetRandomNumber() function
twice to simulate rolling two six-sided dice.
- We want to allow our client to set the range of numbers that
GetRandomNumber() would produce. Instead of passing,
extra parameters to GetRandomNumber() we will use
a new feature of C called global variables. We traced
through a program that
illustrates the lexical scoping of global and local variables
in C.
- Then we wrote a new version of our
program which has a SetRandomRange function.
The output of the program is
still the same as the previous version.
- We broke up our program into three pieces:
the header file,
the implementation of
the functions, and the main
program. These programs can be compiled separately, and
we can put the implemented functions in a library.
See sample run.
- We modified the craps program from Project 1 to use the
random number library instead of taking input from the user.
This required very little change. Instead of calling GetInteger,
we use GetRandomNumber. The resulting
program is compiled with
the random number library. See
sample run.
- It turns out that the rand() function that we
used is not very good, because the lower digits of the
generated numbers are predictable. Since we used the
modulo operator to reduce the range of the random number,
this means the random numbers we generated may be quite
predictable. Again, we consulted the
manual pages of a different
random number generator. According to the manual the
random() function produces numbers where every digit
appears to be random.
- We incorporated the random() and srandom()
functions into a new
implementation of our random number library. This new
library can be linked to our main program without changing or
recompiling the main program because the interface between
these two parts remains the same. (See
sample run.)
- We re-compiled the craps program with the new version of
the random number library without making any changes.
The compiled program runs smoothly. See
sample run.
Last Modified:
Fri Oct 27 17:21:49 EDT 1995
Richard Chang, chang@gl.umbc.edu