Random numbers in parent/child?

Hi

I'm trying to generate random numbers both in parent process and the child process. But I get the same random number in both processes. Why doesn't it generate different numbers altough I seed random number generator?
Here's my code:

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

void seedit(void)
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    srand(tv.tv_sec * tv.tv_usec);
}

int main()
{
        int pid;
        seedit();
        pid = fork();
        if (pid==0)
        {
                int r = rand()%10 + 1;
                printf("[CHILD] sleeping %d sec\n", r);
                sleep(r);
        }
        else if (pid>0)
        {
                int r = rand()%10 + 1;
                printf("[PARENT] sleeping %d sec\n", r);
                sleep(r);
        }
        else
        {
        }
}

Probably the srand/rand functions use some kind of internal state which is bound to the process. When you call fork, the whole process is duplicated, including the state of the randomizer. Either seed it for each process seperately, or use some independent random source (like /dev/random).

Because the child process has the exact same seed value as the parent.

Move the srand statment down inside the if(pid ... ) blocks.
Use two different prime numbers say: 13 and 8191. Then you will get different values.
Time as a ssed may not necessarily work - you can try calling seedit() down inside the if blocks if you want to test how well it works.