Search code examples
crandomsrand

If I don't specify srand(), what seed does rand() use?


Here is the code:

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

int main(void)
{
    int r;
    int i;
    for (i = 0; i < 100; i++)
    {
        r = rand() % 100 + 1;
        printf("%d\n", r);
    }
    return 0;
}

I've been trying to random number but one day, I forgot to put srand() in, but the rand() function can still random a number (the same sequence).

The question is, what seed does it use if I don't specify it?


Solution

  • If srand is not called, rand acts as if srand(1) has been called.

    http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.13.html#rand