I'm using MPICH2 to implement an "Odd-Even" Sort.
I did the implementation but when I randomize to each process his value,
the same number is randomized to all processes.
Here is the code for each process, each process randomized his value..
int main(int argc,char *argv[])
{
int nameLen, numProcs, myID;
char processorName[MPI_MAX_PROCESSOR_NAME];
int myValue;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&myID);
MPI_Comm_size(MPI_COMM_WORLD,&numProcs);
MPI_Get_processor_name(processorName,&nameLen);
MPI_Status status;
srand((unsigned)time(NULL));
myValue = rand()%30+1;
cout << "myID: " << myID << " value: " << myValue<<endl;
MPI_Finalize();
return 0;
}
why each process get the same value?
Edit : thanks for the answers :)
I changed the line
srand((unsigned)time(NULL));
to
srand((unsigned)time(NULL)+myID*numProcs + nameLen);
and it gives a different values for each process :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…