troopers97
troopers97

Reputation: 91

Generating random numbers which allows some repetition within a given range

I'm writing an MPI program where I'm allowing each process to generate it's own random number within an elapsed time where i would want some process to have identical random numbers generated with some other process. For example, a simple printf statement:

Process 1 has generated the number 19
Process 2 has generated the number 8
Process 3 has generated the number 19
.
.

Therefore, process 1 and 3 generated the same random number. Also, to ensure that i can obtain identical random number generated, i will constraint to only integer values, not doubles or floats.

I've written:

#include <stdio.h>
#include "mpi.h"
#include <stdlib.h>
#include <time.h>
#define numprocess 20

int main(int argc, char *argv[])
{
    int rank,size;
    int rand_value;
    int lower,upper;

    MPI_Status status;

    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD,&size);

    upper = 20;
    lower = 0;
    rand_value = rand()%(upper-lower);
    printf("Process %d generated a random number of %d\n",rank,rand_value);

    MPI_Finalize();
    return 0;
}

but I'm getting the output of:

Process 0 generated a random number of 7
Process 2 generated a random number of 7
Process 8 generated a random number of 7
Process 1 generated a random number of 7
Process 3 generated a random number of 7
Process 4 generated a random number of 7
Process 5 generated a random number of 7
Process 6 generated a random number of 7
Process 7 generated a random number of 7
Process 9 generated a random number of 7
Process 11 generated a random number of 7
Process 10 generated a random number of 7
Process 16 generated a random number of 7
Process 17 generated a random number of 7
Process 19 generated a random number of 7
Process 13 generated a random number of 7
Process 12 generated a random number of 7
Process 18 generated a random number of 7
Process 15 generated a random number of 7
Process 14 generated a random number of 7

Upvotes: 1

Views: 417

Answers (1)

klutt
klutt

Reputation: 31389

You could do like this:

srand(time(NULL)+rank * 10000);

Using time will ensure that you get a different output on each run, and using rank makes sure (with high probability but not 100%) that each thread has a different seed. The constant 10000 is an arbitrary constant just to decrease the probability that two threads from accidentally gets the same seed.

But this is a far better method:

const int current_time = time(NULL);

MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);

srand(current_time+rank);

Or using MPI_Bcast

int current_time;

MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);

if(rank == 0)
    current_time = time(NULL);

MPI_Bcast(&current_time, 1, MPI_INT, 0, MPI_COMM_WORLD);
srand(current_time+rank);

Upvotes: 3

Related Questions