Why do I always get the same sequence of random numbers with rand()?

前端 未结 12 2400
忘了有多久
忘了有多久 2020-11-21 06:06

This is the first time I\'m trying random numbers with C (I miss C#). Here is my code:

int i, j = 0;
for(i = 0; i <= 10; i++) {
    j = rand();
    printf         


        
12条回答
  •  长情又很酷
    2020-11-21 06:29

    There's a lot of answers here, but no-one seems to have really explained why it is that rand() always generates the same sequence given the same seed - or even what the seed is really doing. So here goes.

    The rand() function maintains an internal state. Conceptually, you could think of this as a global variable of some type called rand_state. Each time you call rand(), it does two things. It uses the existing state to calculate a new state, and it uses the new state to calculate a number to return to you:

    state_t rand_state = INITIAL_STATE;
    
    state_t calculate_next_state(state_t s);
    int calculate_return_value(state_t s);
    
    int rand(void)
    {
        rand_state = calculate_next_state(rand_state);
        return calculate_return_value(rand_state);
    }
    

    Now you can see that each time you call rand(), it's going to make rand_state move one step along a pre-determined path. The random values you see are just based on where you are along that path, so they're going to follow a pre-determined sequence too.

    Now here's where srand() comes in. It lets you jump to a different point on the path:

    state_t generate_random_state(unsigned int seed);
    
    void srand(unsigned int seed)
    {
        rand_state = generate_random_state(seed);
    }
    

    The exact details of state_t, calculate_next_state(), calculate_return_value() and generate_random_state() can vary from platform to platform, but they're usually quite simple.

    You can see from this that every time your program starts, rand_state is going to start off at INITIAL_STATE (which is equivalent to generate_random_state(1)) - which is why you always get the same sequence if you don't use srand().

提交回复
热议问题