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?

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.

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)

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.

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)

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

## 7 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/520574-does-using-rng-guarantee-having-exactly-the-same-result-in-each-simulation#comment_834776

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/520574-does-using-rng-guarantee-having-exactly-the-same-result-in-each-simulation#comment_834776

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/520574-does-using-rng-guarantee-having-exactly-the-same-result-in-each-simulation#comment_834783

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/520574-does-using-rng-guarantee-having-exactly-the-same-result-in-each-simulation#comment_834783

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/520574-does-using-rng-guarantee-having-exactly-the-same-result-in-each-simulation#comment_834787

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/520574-does-using-rng-guarantee-having-exactly-the-same-result-in-each-simulation#comment_834787

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/520574-does-using-rng-guarantee-having-exactly-the-same-result-in-each-simulation#comment_834797

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/520574-does-using-rng-guarantee-having-exactly-the-same-result-in-each-simulation#comment_834797

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/520574-does-using-rng-guarantee-having-exactly-the-same-result-in-each-simulation#comment_834805

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/520574-does-using-rng-guarantee-having-exactly-the-same-result-in-each-simulation#comment_834805

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/520574-does-using-rng-guarantee-having-exactly-the-same-result-in-each-simulation#comment_835154

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/520574-does-using-rng-guarantee-having-exactly-the-same-result-in-each-simulation#comment_835154

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/520574-does-using-rng-guarantee-having-exactly-the-same-result-in-each-simulation#comment_835163

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/520574-does-using-rng-guarantee-having-exactly-the-same-result-in-each-simulation#comment_835163

Sign in to comment.