mardi 16 juin 2015

Random number generator using uniform distribution

I am generating random numbers within a loop, and therefore the srand() function will not work because it only updates every second. I am using a more robust implementation using the #include random.h directory. I have to generate 4 sets of random arrays of 10 numbers every iteration of the loop. The first two arrays must contain values between 1 and 5 and the latter two must be between 0 and 19.

I am getting an error when I define the second uniform distribution: uniform_int_distribution<> dis(0, 19). The error I am getting says that 'dis' is a redefinition and multiple initialization. So how can I reinitialize a distribution to a new set of numbers using this method?

If you have a better way of implementing, please advice.

    // Here is a more robust implementation of a random number generator 
    // Random seed
    random_device rd;

    // Initialize Mersenne Twister pseudo-random number generator
    mt19937 gen(rd());

    int interact1_random[11]; //Store array of random number for interacting group 1
    int interact2_random[11]; //Store array of random number for interacting group 2
    interact1_random[10] = '\0'; //set the end of the string
    interact2_random[10] = '\0'; //set the end of the string


    int agents1_index_rand[21]; //Store array of random number for ith index of interaction in interactin group 1
    int agents2_index_rand[21]; //Store array of random number for ith index of interaction in interactin group 2
    agents1_index_rand[20] = '\0'; //set the end of the string
    agents2_index_rand[20] = '\0'; //set the end of the string

    // Generate pseudo-random numbers
    // uniformly distributed in range (1, 5)
    uniform_int_distribution<> dis(1, 5);

    // Generate 10 pseudo-random numbers for interacting group 1
    for (int i = 0; i < 10; i++) // ten iterations of the game 
    {
        int randomX = dis(gen);
        interact1_random[i] = randomX;
    }

    // Generate 10 pseudo-random numbers for interacting group 2
    for (int i = 0; i < 10; i++)
    {
        int randomX = dis(gen);
        interact2_random[i] = randomX;
    }

    // Generate pseudo-random numbers
    // uniformly distributed in range (0, 19)
    uniform_int_distribution<> dis(0, 19);
    // Generate 20 pseudo-random numbers for the index of interaction, interacting in group 1
    for (int i = 0; i < 10; i++)
    {
        int randomX = dis(gen);
        agents1_index_rand[i] = randomX;
    }
    // Generate 20 pseudo-random numbers for the index of interaction, interacting in group 2
    for (int i = 0; i < 10; i++)
    {
        int randomX = dis(gen);
        agents2_index_rand[i] = randomX;
    }




Aucun commentaire:

Enregistrer un commentaire