generate an independent seed with a fixed number

2 ビュー (過去 30 日間)
Ebru Angun
Ebru Angun 2022 年 6 月 17 日
コメント済み: Peter Perkins 2022 年 6 月 21 日
Hi to all,
I am simulating a simple problem in Matlab and optimizing it through OptQuest. The function in Matlab is as follows:
function saddleproduct(infile, outfile, replication)
% Read input file
inp = readtable(infile);
disp(infile);
disp(outfile);
disp(replication);
disp(inp);
%Table indices are 1-based
var1 = inp{1,2};
var2 = inp{2,2};
%variance for simulation: Jalali et al.
W_expected(1) = var1 + var2;
W_expected(2) = 1.5-var1-2*var2-(0.5)*sin(2*pi*(var1^2-2*var2));
W_expected(3) = var1^2+var2^2-1.5;
variances = [(0.45*W_expected(1)+0.3)^2 (0.45*W_expected(2)+1.15)^2 (0.45*W_expected(3)+0.98)^2];
%simulate at current point
w0 = var1 + var2 + normrnd(0, sqrt(variances(1)));
w1 = 1.5 - var1 - 2*var2 -0.5*sin(2*pi*(var1^2 - 2*var2)) + ...
normrnd(0, sqrt(variances(2)));
w2 = var1^2 + var2^2 -1.5 + normrnd(0, sqrt(variances(3)));
%outputs
product=w0;
sum = w1;
quotient=w2;
% Output results to file
output = table({'func-product';'func-sum';'func-quotient'}, {product;sum;quotient});
disp(output);
writetable(output, outfile, 'WriteVariableNames', false);
end
The function reads inputs, gets replication number from OptQuest, and writes output to a file. In simulation (i.e., normrnd), Matlab always uses the default seed (rng('default)). I have to change the code in such a way that Matlab uses replication to determine an independent seed for each replication, where replications at the same input has the same number of replications.
How should I use the replication number to create a seed independent for each replication at the same input? Is clock the only option for it? What is your recommendation?

採用された回答

Peter Perkins
Peter Perkins 2022 年 6 月 17 日
Ebru, you are doing parallel simulations and presumably want to combine the results under the assumption that those results are (pseudo)independent across replications.
Don't use seeds for that. You can, but there are better ways. Take a look at the parallel generators that support streams and substreams, and use one of those. These strategies are well documented, see
  4 件のコメント
Peter Perkins
Peter Perkins 2022 年 6 月 21 日
The whole point of stream numbers and substream numbers is that you don't need to save any states. Each stream or substream is easily referenced by its number. Read those doc sections.

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

その他の回答 (1 件)

Jan
Jan 2022 年 6 月 17 日
編集済み: Jan 2022 年 6 月 17 日
rng('default')
is the default seed at the start of the Matlab session. All subsequent requesrts of random numbers move the seed accordingly. There is no need to seed the generator repeatedly.
If you wat the initial seed to be random also, use
rng('shuffle')
If you want the seed to depend of the loop index:
rng(replication)
assuming that replication is a positive integer.
But I'm confused by the question:
"How should I use the replication number to create a seed independent for each replication at the same input?" - with useing a defined seed, the rng is not "independent", but dependent.
"Is clock the only option for it?" - Why? If the rng should be independent, do not set further seeds.
  5 件のコメント
Ebru Angun
Ebru Angun 2022 年 6 月 17 日
Thank you Jan.

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

Community Treasure Hunt

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

Start Hunting!

Translated by