フィルターのクリア

Does Matlab initialize random seed at each -batch run?

10 ビュー (過去 30 日間)
Chern Hui Lee
Chern Hui Lee 2021 年 10 月 12 日
編集済み: Jan 2021 年 10 月 12 日
I have 2 scripts, let's call it ScriptA.m and ScriptB.m. ScriptB.m is supposed to be the same as ScriptA.m except with improvement on the algorithm for speed. Both scripts make use of a lot of random numbers, but at different places due to the speed improvement of ScriptB.m. I want to make sure that statistically the two scripts produce the similar result, so I run each one of them 5x using matlab -batch 'run xxx.m', and look at the variation from run-to-run and script-to-script. I noticed that the output of each script is exactly identical across the 5x runs, which is a little strange to me. Does this mean Matlab initialize the random seed for each -batch run? Note that I do not use the function rng in my script for seed initialization. Thanks.
  1 件のコメント
Chern Hui Lee
Chern Hui Lee 2021 年 10 月 12 日
I realized I can just run the following to confirm that it indeed uses the same random seed each time
matlab -batch 'rng'
and I always get the same seed 0. Pardon the stupid question.

サインインしてコメントする。

採用された回答

Chern Hui Lee
Chern Hui Lee 2021 年 10 月 12 日
matlab -batch 'rng'
shows that it indeed uses random seed 0 every time.

その他の回答 (1 件)

Jan
Jan 2021 年 10 月 12 日
編集済み: Jan 2021 年 10 月 12 日
Yes, exactly as you observe the random number generator is initialized with the same seed.
Use rng to get different seeds. See:
doc rng
"To create one or more independent streams separate from the global stream, see RandStream and RandStream.create. "

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by