MPI C random seeds

/* C MPI example */

/* to run, put the contents in a file, "rand.mpi.c" and run

mpicc rand.mpi.c

mpirun -np 2 ./a.out

*/

#include <stdio.h>

#include <mpi.h>

int main (argc,argv)

int argc;

char *argv[];

{

int rank, size;

int this_delay,delay_low, delay_high;

delay_low=1;

delay_high=5;

MPI_Init (&argc, &argv);

MPI_Comm_rank (MPI_COMM_WORLD, &rank);

MPI_Comm_size (MPI_COMM_WORLD, &size);

printf(" Hello from process %d of %d\n", rank, size);

srand(rank+1); // each instance should get a different random seed

printf("rand on %d = %d\n",rank, rand());

printf("rand on %d = %d\n",rank, rand());

this_delay = rand() % ((delay_high - delay_low + 1) + delay_low);

printf("rand in range = %d\n", this_delay);

sleep(this_delay); // 1000 microseconds = 1 second

printf("done\n");

MPI_Finalize();

return 0;

}