rng('default') unreliable
13 ビュー (過去 30 日間)
古いコメントを表示
I have some lengthy code (too long to post here) which executes some sampling based computations. At the beginning of the code, I set
rng('default')
to ensure reproducibility of results for debugging purposes (afaik, this means a Mersenne twister with seed 0). This is the only place in the code where rng is invoked. During program execution, some quantities are sampled using normrnd, which are subsequently used as inputs for the computations. The thing is, I've noticed on multiple occasions now that running the exact same code several times does NOT always yield the exact same results.
I always have one instance of Matlab running in GUI mode. Alternatively, I can also run the code in batch mode in the background, which I do most of the time. Although I change absolutely nothing in the code, the sample values occasionally (not always) differ from run to run. I can't figure out what's going on, but since my input does not change, it must have something to do with the rng. Has someone experienced the same problem? Could it be some kind of crossfire between GUI and batch mode? Or does rng('default') involve time or some other "random" variable?
0 件のコメント
回答 (2 件)
Jan
2022 年 6 月 13 日
編集済み: Jan
2022 年 6 月 13 日
rng('default') sets the random number generator to the initial state. This is a reliable procedure.
If your code replies different results for the random numbers, it must contain another source of randomness. If this effect occurs only sometimes, it does not seem to be a problem related to the value of the current time. This would be something like:
% Not likely to be the problem:
itime = now;
while now - itime < 5
a = rand;
end
Such dependencies on the time should be observable in all runs.
Another problem can be using different inputs relying ob the path "D:" : This is the last folder used on the disk D and can be influenced by external software. Brrr, such evil.
I assume, you have to check the code and search for the source of the problem. While the RNG is known not to create magical output, you either process different inputs (without knowing it) or there is another source of entropy.
8 件のコメント
Steven Lord
2022 年 6 月 15 日
Or you could sort the output of fieldnames before deciding which field gets which random vector if you don't need or want to reorder the struct array itself.
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!