If you don't have a working compiler, then how do you know what the first line will be?
Back in school in the late 70's, I decided to teach myself BASIC, since there were no classes offered in it; most of our programming then was in PL/I. So I familiarized myself with the school computer's BASIC (VSPC BASIC on an IBM S/370) and I bought David Ahl's book of computer games in BASIC. When I played with Humurabi wherein you try to manage a Mespotamian city-state, a plague hit the city every single year. Such events were determined by the RND function and I had typed it in just as I was supposed to. Well, it turned out that in the book's version of BASIC (Microsoft -- they started out as a language company supplying BASIC to the fledgling home computer market) you were to pass a 1 to RND, but in the VSPC BASIC that I was using you needed to pass it a zero. In VSPC BASIC, passing in any non-zero argument would cause RND to generate a specific random number (which just happened to be the one for plague), whereas what I needed was a different random number each time. That is why my city-state was plagued by the plague every single year. I had to read the VSPC BASIC manual to figure that one out; there was no Google in those days.
RND is like rand() in that it uses a pseudo-random number generator
(PRNG -- see Wikipedia at http://en.wikipedia.org/wiki/PRNG
). What a PRNG does is to perform a calculation (often a large multiplication or division followed by a modulo operation) based on a previous value. That previous value is first set by srand() and thereafter the PRNG uses the last pseudo-random number that it had generated.
The sequence appears to be random, but if you start with the same seed value as before then you will generate the exact same "random" sequence of numbers.
For that reason, to get a considerably more random sequence, you need to pass a unique value to srand(). For that reason, it is customary to pass the return value of time() to srand(), since that time value is the number of seconds since midnight 1970 Jan 01 and hence is unique after a second of time.
Now, it can be useful to feed a known sequence of "random" numbers to a program that you are testing and/or debugging. To do that, you simply feed srand() a specific number and record what the calls to rand() return. Then when you call srand() with that same specific number you will get that same sequence of "random" numbers.
That is what your program is doing. You already know what values rand()%5
will give you, so apply that knowledge to your problem.
Originally Posted by Michael Carl
I know it's not good to rely on, but I've read over the srand function section in my text book like ten times and I'm just not seeing any relations to the problem.
When your textbook is not enough, then R
). The man pages (the on-board documentation in UNIX) are posted all over the place on-line; Google on man page srand
and you will find "About 38,700 results", the first one being at http://linux.die.net/man/3/srand
, which says in part (emphasis added):
| Description |
The rand() function returns a pseudo-random integer in the range 0 to RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]).
The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value.
If no seed value is provided, the rand() function is automatically seeded with a value of 1.
It even offers an implementation of rand() and srand():
/* Example */
POSIX.1-2001 gives the following example of an
implementation of rand() and srand(), possibly useful
when one needs the same sequence on two different
static unsigned long next = 1;
/* RAND_MAX assumed to be 32767 */
next = next * 1103515245 + 12345;
return((unsigned)(next/65536) % 32768);
void mysrand(unsigned seed)
next = seed;
So when you find your textbook's explanation of a standard library function lacking, RTFM!