Search code examples
csrand

Using srand() within a function


I am learning how to use "rand()" and "srand()" in C. In the book that I am reading, it asks to modify a program that generates random numbers so that the user supplies a seed. This ensures the numbers are random. I am able to generate new numbers using the basic code directly below, but it will not work when I try to use it within the program beneath it.

int main() 
{
    int seed;

    srand(seed);

    printf("Please enter a seed: ");
    scanf("%d", &seed);

    printf("The random number is: %d\n", rand());

    return (EXIT_SUCCESS);
}

Here is the program I am having trouble getting "srand() to work in below. I have tried asking the user for the seed in "main", rather than the prn_random_numbers function and everything else I could think of. It still only behaves like "rand()" and spits out the same number regardless of the seed that I enter. What am I doing wrong? Unfortunately, the book doesn't give any answers to the exercises. I greatly appreciate any help. I am just learning on my own.

max(x, y)
int x, y;
{
    if (x > y)
        return (x);
    else
        return (y);
}

min(x, y)
int x, y;
{
   if (x < y)
       return (x);
   else
       return (y);  
}

prn_random_numbers(k)   /* print k random numbers */
int k;
{
    int i, r, smallest, biggest;
    int seed;

    srand(seed);
    printf("Please enter a seed: ");
    scanf("%d", &seed);

    r = smallest = biggest = rand();
     printf("\n%12d", r);
    for (i = 1; i < k; ++i)
    {
        if (i % 5 == 0)
            printf("\n");
        r = rand();
        smallest = min(r, smallest);
        biggest = max(r, biggest);
        printf("%12d", r); 
    }
    printf("\n\n%d random numbers printed.\n", k);
    printf("Minimum:%12d\nMaximum:%12d\n", smallest, biggest);
}

int main() 
{
    int n;

    printf("Some random numbers are to be printed.\n");
    printf("How many would you like to see? ");
    scanf("%d", &n);
    while (n < 1)
    {
        printf("ERROR! Please enter a positive integer.\n");
        printf("How many would you like to see? ");
        scanf("%d", &n);
    }        
    prn_random_numbers(n);

    return (EXIT_SUCCESS);
}

Solution

  • Part 1

    In the first block of code, you call srand() with an uninitialized variable — this is not good. You need to move that call after where you read the seed from the user. You should also check that you got a valid result from the input.

    int main(void) 
    {
        int seed;
    
        printf("Please enter a seed: ");
        if (scanf("%d", &seed) != 1)
        {
            fprintf(stderr, "Failed to read seed - exiting\n");
            return EXIT_FAILURE;
        }
        srand(seed);
    

    Part 2

    In the second block of code, you are writing K&R-style function definitions.

    DON'T!!!

    And yes, I would shout louder if I could. If your text book is teaching you this style, hurl the book into the rubbish bin and get another book. You have to have an extraordinary reason to use K&R-style definitions in new code. Prototypes have been available via standard compilers for about thirty years now, and have been generally available everywhere for twenty years.

    Your code calling srand() in the second example also has the same flaw as in the first — calling the function before you get a seed from the user.

    Also, the chances of users giving a different seed each time the program is run are approximately nil. Your claim that it "ensures the numbers are random" is a very optimistic view.