0

I am running two different codes which generate the same amount of random numbers (thousands of them). I was trying to keep the same path of random numbers on both codes using:

rng('default') 
rng(1);

However, I am almost sure that the random numbers being generated are different. Can it be due to the fact that I am using a parfor loop?

Thanks

phdstudent
  • 1,060
  • 20
  • 41
  • 2
    Yes, it is due to the `parfor`. This happens because `parfor` does not iterate the loop in a successive order, but in a random order as distributed per worker. So you do have the same random numbers (probably), but permuted in a random way. – Adriaan Sep 03 '15 at 09:27
  • Hum. Thanks for your comment. Any workaround on that? (e.g. force parfor to iterate loop in successive order?) – phdstudent Sep 03 '15 at 09:28
  • 2
    You can't, that's the nature of `parfor`. If you require your loop to be iterated in the same order, use `for`. Besides, thousands of iterations is very low for `parfor`, see http://stackoverflow.com/questions/32146555/saving-time-and-memory-using-parfor-in-matlab/32146700#32146700 – Adriaan Sep 03 '15 at 09:29
  • 1
    Now that I think of it, what you can probably do (without seeing your code) is to initialise a random array *before* the `parfor`, then let each loop call a value from that array. – Adriaan Sep 03 '15 at 09:34
  • Actually, the parfor on my case allows me to reduce code time from 8 hours to approximately 2 hours, so it is a significant difference. I though about that but there are way to many random variables being generated on my code (around 500*300*2*2). Some are from uniform distributions, some from normals with different variances and means, Also, the result of uniform random generator impacts the variance of the normal that I want to draw next ... – phdstudent Sep 03 '15 at 09:37
  • 2
    @Adriaan, do you think something along the lines of the last post on the thread below would solve it? http://stackoverflow.com/questions/17111111/matlab-how-to-set-random-seed-in-parfor-to-produce-same-results-as-serial-for – phdstudent Sep 03 '15 at 09:46
  • Seem like a duplicate to this post even. Should work. – Adriaan Sep 03 '15 at 09:50

0 Answers0