2

My code initializes a pointer to a random name. The random name is randomly selected by using the rand function.

I have noticed that the rand function does not quite do what I want it to do. In the following code, it initializes all the pointers with the same name! I suspect this has do to with the rand function basing its selection on the time function. If it initializes all the pointers at once, it will produce the same random number!? I'm sure it takes time for the time stamp to produce another number right? I can fix the code by running the initializations in a loop but I would like to know another way to fix as sometimes it takes a few seconds to work through the loop.

For reference:

#include <stdio.h>
#include <stdlib.h>

int  random_number(int, int);
const char *random_name();
//----------------------------------------------------------------
int main(void)
{
    const char * kg = NULL;
    const char * ke = NULL;
    const char * dg = NULL;
    const char * de = NULL;

    kg = random_name();
    ke = random_name();
    dg = random_name();
    de = random_name();

    printf("kg=%s\nke=%s\ndg=%s\nde=%s\n", kg, ke, dg, de);

    return 0;
}
//----------------------------------------------------------------
const char *random_name(int x)
{
    const char *names[7] =
    {"Bob", "Billy", "Buck", "Bobby", "Bill", "Billy Bob", "Bobi"};

    int roll = random_number(0,7);
    return names[roll];
}
//----------------------------------------------------------------
int random_number(int min, int max)
{
    int roll;
    int maximum = max - min;
    srand(time(NULL));
    roll = (rand() % maximum) + min;
    return roll;
}
icedwater
  • 4,701
  • 3
  • 35
  • 50
Samuel
  • 45
  • 5
  • 6
    Don't seed before every call to `rand()`. Just call `srand` once when the program starts. – Anthony Jun 19 '13 at 01:02
  • 1
    Please also consider using more meaningful variable names. – icedwater Jun 19 '13 at 01:04
  • I use much more meaningful variable names in my final program. And the reason those are short and meaningless is because they don't have comments and are supposed to be short because I will use them a hundred times. But I too believe variables should have good names. – Samuel Jun 19 '13 at 02:57

4 Answers4

7

The rand() function is a pseudorandom number generator that uses the previous generation to calculate the next value. By seeding with srand() you are setting the starting value and if you do multiple times you effectively 'reset' the random number sequence. Seed the random number generator once (using srand()) one at the start of your program instead of before each call to rand().

Chad
  • 18,706
  • 4
  • 46
  • 63
2

Move srand(time(NULL)) into your main method. That seeds the random number generator at the program's entry point, allowing for a different random number with an even distribution from the rest to be retrieved each time rand() is called.

2

I believe the issue lies in the fact that you seed each call to the PRNG with a new value on each run. See what srand() does here. The reason you would want to avoid seeding the PRNG each call is because then you cannot guarantee that the value generated on the next call to rand() is evenly distributed. What you're most likely looking for is that even distribution of values, and using srand() in this way will yield unexpected results. This answer will provide more details on why you should only call srand() once.

Also bear in mind that similar rules would apply to any PRNG in any language, and not just C.

Community
  • 1
  • 1
pseudoramble
  • 2,541
  • 22
  • 28
2

Your program runs very quickly, so time returns the same value each time it is called. Since the value passed to srand (the s stands for "seed") determines what sequence rand will produce, it produces the same number (the initial number or an otherwise-random sequence) each time within the same program run, although the number should vary when you run the program again a second later.

The solution, as others have mentioned, is to call srand at the beginning of main instead.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421