Does using rng guarantee having exactly the same result in each simulation?

I am using rng to be able to reproduce data in each simulation. But I noticed there is a slightly difference for the result in each simulation (around 1%). Is it normal?

7 commentaires

I have a hypnosis that it just might be the case that sum() of a large vector might not always be the same. For sufficiently large arrays it would be handed to a high performance library that breaks the problem into sections and uses different threads to calculate subtotals for the different sections, and then combines the subtotals. But hypothetically the subtotals might get added in the order that the threads finish, which is not deterministic. However it is also possible that the software waits for all the threads to finish, so this is a hypothesis not certainty.
I think in theory prod() could have the same effect; the round-off for (A.*B).*C is not necessarily the same as for A.*(B.*C)
@walter Rberson:
I am using rng in a loop sth like this:
for i=1:..
for rng_idx=10:-1:1
rng(rng_idx,'twister');
...
end
end
for i=1 and rng_idx=1 I get the same answer compared to when not having for loop for rng_idx and with setting rng(1). But for the rest, I get different answer e.g. I mean for i=2 and rng_idx=1 I get different answer when I used for i=2 without any loop for rng and with setting rng(1)
Would resetting rng in a for loop be the reason?
"Would resetting rng in a for loop be the reason?"
Yes.
Seeding rng with different values means it will generate different outputs. That is the point of it.
"Does using rng guarantee having exactly the same result in each simulation?"
If you call it with exactly the same seed value and generator, then yes.
But as you are using it with a different seed on each loop iteration, then no.
If you call it with exactly the same seed value and generator, then yes.
I am not convinced that is always true when MATLAB automatically invokes the multithreaded high-performance libraries. It might be the case that those libraries take care to handle partial results in a consistent order, but I do not know if they do.
@Stephen Cobeldick:
My qustion was that when I use rng(1) in the loop I get different answer compared to when I use rng(1) on its own (around 1% of difference).
@Walter Roberson:
I edited the comment and added clearer explanation (more clue):
I am using rng in a loop sth like this:
for i=1:..
for rng_idx=10:-1:1
rng(rng_idx,'twister');
...
end
end
for i=1 and rng_idx=1 I get the same answer compared to when not having for loop for rng_idx and with setting rng(1). But for the rest, I get different answer e.g. I mean for i=2 and rng_idx=1 I get different answer when I used for i=2 without any loop for rng and with setting rng(1)

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Discrete Multiresolution Analysis dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by