简体   繁体   中英

Rand function improvement

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;
}

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() .

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM