Random number generator

Can anyone please explain me what is happening over here.

In this sample code...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void generate()
{
    int i, op;
    srand((unsigned)time(NULL));
    for(i = 0; i < 10; i++)
    {
        op = rand() % 3;
    }
}

int main()
{
    int i;
    for(i = 0; i < 2; i++)
    {
        generate();
    }
    return 0;
}


Two series of random numbers are same.

But

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void generate()
{
    int i, op;

    for(i = 0; i < 10; i++)
    {
        op = rand() % 3;
    }
}

int main()
{
    int i;
    srand((unsigned)time(NULL));
    for(i = 0; i < 2; i++)
    {
        generate();
    }
    return 0;
}


in this code they are different? why so? How does srand() behaves?
Line 14 on your second piece seeds the generator with the number of seconds since midnight.

http://en.wikipedia.org/wiki/Random_seed
Topic archived. No new replies allowed.