1

How can I generate in parallel (is it efficient? possible?) random variables with my linear congruential generator below:

double* uniform(long N)
{
long i,j;
long a=16807;
long long m=(((long long)1)<<31)-1;
long I[N];
double *U;

#pragma omp parallel for  firstprivate(i)
for (j = 0; j < N;j++)
{
    if (i==0)
    {
        int y= omp_get_thread_num(); // undefined ref error here
        I[y];
        i++;
    }
    else
    {
        I[j] = (a*I[j-1])%m;
    }

}

#pragma omp parallel for
for (i=0; i<N; i++)
    U[i] = (double)I[i]/(m+1.0);

return U;
}

My goal is to generate 2 variables to use them in another function (box-muller method):

double* gauss(long int N)
{
    double *X, *Y, *U;
    X = generator(N/2);
    Y = generator(N/2);
    #pragma omp parallel for
    for (i=0;i<N/2;i++)
    {
        U[2*i]=sqrt(-2 * log(X[i]))*sin(Y[i]*2*3.14);
        U[2*i+1]=sqrt(-2 * log(X[i]))*cos(Y[i]*2*3.14);
    }
    return U;
}

How want to know how can I get different seeds when generating uniform variables with the function uniform?

dowjie
  • 95
  • 1
  • 6
  • The question here is; what for? What is your task? There are faster PRNGs providing better randomness (e.g. xoroshiro) and more important: there are better approaches than box-muller (e.g. ziggurat). At least the latter is most probably implemented in something like GSL or other libraries. So there is some potential in regards to speedup & accuracy (of your sampling) without using parallelization. – sascha Jan 15 '17 at 17:52
  • @sascha My task is to generate standard normal variables using parallelization. My first thought was to use a linear congruential generator. – dowjie Jan 15 '17 at 18:23
  • And what did you try yet? And more important: did you check all the other questions on SO like [this](http://stackoverflow.com/questions/4287531/how-to-generate-random-numbers-in-parallel)? – sascha Jan 15 '17 at 18:28
  • @sascha Checked but it's in C but I edited my work, I get an error with omp_get_thread_num – dowjie Jan 15 '17 at 18:51

0 Answers0