mardi 18 juillet 2023

Can't reproduce normally distributed random numbers in parallel

I want to generate a reproducible sequence of random numbers in parallel with OpenMP. I wrote a small code that creates for each thread its own RNG with different seed. But when I start generating random numbers in parallel, something strange happens. With uniform distribution I get reproducible results (rand() here to generate same sequence of seeds whenever i launch the program), but with normal distribution i get different results!

More specifically, every time I run the program, the array seems to be shuffled.

My question: Why this might be happening? And how do I resolve this issue?

#include <iostream>
#include <random>
#include <omp.h>

int main() {
    std::normal_distribution<> normal(0,1);
    std::uniform_real_distribution<> uniform(0,1);
    std::mt19937 *rng = new std::mt19937[omp_get_max_threads()];
    for(int i = 0; i < omp_get_max_threads(); ++i) {
        rng[i].seed(1+i);
    }

    int N = 100;
    double* arr = new double[N];
#pragma omp parallel for
    for(int i = 0; i < N; ++i) {
        arr[i] = normal(rng[omp_get_thread_num()]);
    }
    for(int i = 0; i < N; ++i) {
        std::cout << arr[i] << std::endl;
    }

    delete[] arr;
    delete[] rng;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire