#include #include #include #include int main(int argc, char** argv) { // Initialize the MPI environment MPI_Init(&argc, &argv); // Get the rank of the process and the total number of processes int world_rank, world_size; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); MPI_Comm_size(MPI_COMM_WORLD, &world_size); // Seed the random number generator to get different results each time srand(time(NULL) + world_rank); // Generate a random number at each process int rand_num = rand() % 100; printf("Process %d generated number %d\n", world_rank, rand_num); // Use MPI_Reduce to sum all random numbers and send the result to the root process int sum; MPI_Reduce(&rand_num, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); // The root process prints the result if (world_rank == 0) { printf("The sum of all random numbers is %d\n", sum); } // Finalize the MPI environment MPI_Finalize(); }