0

I am trying to draw some random points, and then calculate smth with them. I am using few threads, but my random is not so random as it supposed to be... I mean when I am using rand() I gets correct answer, but very slow(because of static rand), so I am using rand_r with seed, but the answer of my program is always wird.

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

my program:

#pragma omp parallel for(int i = 0; i < 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 some solution in other topics(some mt19937 generators etc), but i cant compile anything.

I am using g++ -fopenmp for compiling.(g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2)

edit:

seed = rand(); #pragma omp parallel for(int i = 0; i < points; i++){ punkt.x = randomNumber(seed, minX, maxX); punkt.y = randomNumber(seed, minY, maxY); ... } 

2 Answers 2

1

Re-seeding your generators within each iteration of the for loop is going to ruin their statistical properties.

Also, it's likely that you'll introduce correlation between your x and y values if you extract them using two linear congruential generators.

Keep it simple; use one generator, and one seed.

Going forward, I'd recommend you use mt19937 as it will have better properties still. Linear congruential generators can fail a chi squared test for autocorrelation which is particularly important if you are using it for an x, y plot.

Sign up to request clarification or add additional context in comments.

5 Comments

I need using multy threads cause it is task for very big number of points, without threads it spending too much time. When I am using only seedX for both it also giving some wird answers. :(
It will do if you seed more than once.
I am not sure if I understood properly. I updated code in first comment. It still doesn't works. For one seed variable which is calculated in each iteration. I even added thread number for seed in each thread
For the third time, seed exactly once. Can you not see that you're re-seeding on every iteration? What do you think seeding achieves?
So I generated seed before loop(edit code) and now it is drawing same numbers everytime
0

I believe that others are trying to say is, seed one in constructor with srand(some number), then do not seed anymore.

class someRandomNumber {

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.