Random Number to each Process in MPI
This task is not trivial.
You are getting same numbers because you initialize srand()
with time(0)
. What time(0)
does is return current second (since epoch). So if all the processes have syncronized clocks all will initialize with the same seed as long as they call srand()
on the same second, which is pretty probable. I have observed this even on large machines.
Solution 1. Use local values to initialize random seed.
What I did was to include into computing random seed some memory usage from cat /proc/meminfo
combined with /dev/random
, which are more local to physical machine than clocks. Note that this might still fail for N tasks on 1 machine. But if I recall correctly I also used task_id
. Anything that is local to task will suffice. Combining stuff is also good idea. After all this computations should be very short compared to real computations. And its better to stay on the safe side.
Solution 2. Compute seeds as pre-processing step.
You could also generate random seeds from task 0
using your method and propagate it with send-to-all
. Though, it might have scaling troubles when going huge scale (like 10^5 processes). You could also use any other method to load parameters and just prepare seeds as a pre-processing step. However it also involves some non-trivial work.
It's because your seed does not change enough and the randomness depends on your seed.
From the srand
docs:
For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand.
EDIT: Try generating the seed beforehand or change the seed by hand for every srand
call.