#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Srand quick problem


    Can someone please explain what will be printed here?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    main()
    {
    	int i, sum;
    	srand(1234);
    	for (i=0; i<5; i++)
    		printf("%d  ", 5 + rand()%5);
    	printf("\n");
    	srand(1234);
    	sum = 0;
    	for (i=0; i<5; i++)
    		sum += (rand()%5);
    	printf("average = %5.2f\n", (float)sum / 5);
    
    }
    Apparently, there will be two lines and the first is:
    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    886
    First go back an put your code inside code blocks.
    I no longer wish to be associated with this site.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,089
    Rep Power
    2222
    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.

    PS
    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 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):
    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():
    Code:
    /* 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 
      machines. 
    */
    
    static unsigned long next = 1;
    
    /* RAND_MAX assumed to be 32767 */
    int myrand(void) 
    {
        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!
    Last edited by dwise1_aol; April 16th, 2013 at 07:10 PM.

IMN logo majestic logo threadwatch logo seochat tools logo