//randoms.c - Here's a convenient way to seed random numbers in MPI. //This uses getpid() - gets the process number of the particular process, // and uses this as a seed to srand(). //You'll need to include sys/types.h #include #include #include "mpi.h" int main(int argc, char **argv) { int rank, size; int x; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Who am I? MPI_Comm_size(MPI_COMM_WORLD, &size); // how many in all? srand(getpid()); //This has to go after the MPI_Init(), not before x = rand() % 1000; printf("Hello world. I am %d of %d, my random number is %d, process=%d\n", rank, size, x, getpid()); MPI_Finalize(); return 0; }