#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int i =10;
/* initialize random seed: */
srand(time(NULL));
while(i--){
if(fork()==0){
/* initialize random seed here does not make a difference:
srand(time(NULL));
*/
printf("%d : %d\n",i,rand());
return;
}
}
return (EXIT_SUCCESS);
}
Prints the same (different on each run) number 10 times - expected ? I have a more complicated piece of code where each forked process runs in turn - no difference
This solve the problem:
I havent tested with fork, but inside a for calling 100 times it works.
seed with the pid number. It's a little but difficult to solve problem.
This was in some page: "this worked srand(time(0)+getpid()); but I had to call this within the case 0 i.e child process".
If your code is running fast enough,
srand()
might be seeded with the exact same time for each fork.time()
only changes every second.The
rand()
function is a pseudo-random number generator. This means that the sequence of numbers generated is deterministic, depending only upon the seed provided.Because you are forking the same process 10 times, the state of the random number generator is the same for each child. The next time you call
rand()
you will get the same value.By calling
srand(time(NULL))
inside the child process, you are potentially helping but the granularity oftime()
is only 1 second, so all your children probably start inside the same second. Seeding with the same value generates the same pseudo-random sequence.You could try seeding with a value that depends on the child number:
(I used
i*2
in the event thattime()
advances by 1 second during the fork loop.)You're not reseeding when you make a child process. The state of the random number generator is exactly the same.
Even if you seed again in your child, you're seeding with the time with a +/- 1 second granularity. When you fork, it all happens in less than a second.
Try seeding it with something different and more random.
The reason for this is because all programs are seeded with the same value (outside that while loop). You should seed again once you've forked the new program or both will produce the same sequence.
The outputs must be the same. If two processes each seed the random number with the same seed and each call
rand
once, they must get the same result. That's the whole point of having a seed. All of your processes callsrand
with the same seed (because you only callsrand
once) and they all callrand
once, so they must get the same result.Uncommenting the
srand
won't make a difference because unless the number of seconds has changed, they will still give the same seed. You could do: