April 16th, 2013, 07:10 PM
Srand quick problem
Can someone please explain what will be printed here?
Apparently, there will be two lines and the first is:
int i, sum;
for (i=0; i<5; i++)
printf("%d ", 5 + rand()%5);
sum = 0;
for (i=0; i<5; i++)
sum += (rand()%5);
printf("average = %5.2f\n", (float)sum / 5);
8 8 6 8 6
I need to know what the second line will be, but it'd be helpful if someone could also explain why.
This would be a lot easier if I had a working compiler. 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.
Edit: Sorry about that! Always forget.
April 16th, 2013, 07:13 PM
First go back an put your code inside code blocks.
I no longer wish to be associated with this site.
April 16th, 2013, 07:52 PM
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.
When your textbook is not enough, then Read The Manual (RTFM!). 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):
Originally Posted by Michael Carl
It even offers an implementation of rand() and srand():
So when you find your textbook's explanation of a standard library function lacking, RTFM!
/* 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;
Last edited by dwise1_aol; April 16th, 2013 at 08:10 PM.