multithreading - C++ with OpenMP thread safe random numbers -


i trying draw random points, , calculate smth them. using few threads, random not random supposed be... mean when using rand() gets correct answer, slow(because of static rand), using rand_r seed, answer of program wird.

double randomnumber(unsigned int seed, double a, double b) {     return + ((float)rand_r(&seed))/(float)(rand_max) * (b-a); } 

my program:

#pragma omp parallel for(int = 0; < points; i++){     seedx = (i+1) * time(null);     seedy = (points - i) * time(null);      punkt.x = randomnumber(seedx, minx, maxx);     punkt.y = randomnumber(seedy, miny, maxy); ... } 

i found solution in other topics(some mt19937 generators etc), cant compile anything.

i using g++ -fopenmp compiling.(g++ (ubuntu 4.8.2-19ubuntu1) 4.8.2)

edit:

seed = rand(); #pragma omp parallel for(int = 0; < points; i++){       punkt.x = randomnumber(seed, minx, maxx);     punkt.y = randomnumber(seed, miny, maxy); ... } 

re-seeding generators within each iteration of for loop going ruin statistical properties.

also, it's you'll introduce correlation between x , y values if extract them using 2 linear congruential generators.

keep simple; use 1 generator, , 1 seed.

going forward, i'd recommend use mt19937 have better properties still. linear congruential generators can fail chi squared test autocorrelation particularly important if using x, y plot.


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -