フィルターのクリア

How do I set a seed to generate different random initial numbers and storing them

49 ビュー (過去 30 日間)
Samson
Samson 2020 年 7 月 2 日
コメント済み: Samson 2020 年 7 月 9 日
for kk = 1 : Iter
xD = rand(N,1)*2*pi; % Init Cond. Driver
end
  3 件のコメント
Wiley Mosley
Wiley Mosley 2020 年 7 月 3 日
I think you are wanting a random repeatable setup.
I think the best way to set that up is to review:
Essentially you need to set a random repeatable seed so that you can reinitialize and run with the same random values for refining your code.
rng(1,'twister');
Samson
Samson 2020 年 7 月 3 日
Thank you for your response. However, I do not want the numbers to be repeated. I need to run a100 realizations and having 100 different initial conditions and storing up the initial conditions so I can retrieve any to work

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

採用された回答

Jeff Miller
Jeff Miller 2020 年 7 月 3 日
Maybe the FileExchange contribution RNGMgr will be helpful.

その他の回答 (1 件)

Wiley Mosley
Wiley Mosley 2020 年 7 月 3 日
編集済み: Wiley Mosley 2020 年 7 月 3 日
rng(1,'twister'); % init generator for random repeatable with seed 1
s = rng; % save generator settings as s
for kk = 1: Iter
xD = rand (N, 1) * 2 * pi; % Init Cond. Driver
end
disp(xD) %just to print out your xD values
rng(s) % Reset the generator
for kk = 1: Iter
xD = rand (N, 1) * 2 * pi; % Init Cond. Driver
end
disp(xD) %printing out the xD values again should show that they match
I believe somthing like this should help you.
  9 件のコメント
Samson
Samson 2020 年 7 月 8 日
Hello Walter, thank you so much for this simplified version. However, I did run the N=50 oscillators 5 times, that is iter=5 as a sample. on using rng(rstates{4}); to run and do some plot, my results is below:
for kk = 1 : Iter
%rstates{kk} = rng();
rng(rstates{4});
xD = rand(N,1)*2*pi; % Init Cond. Driver
%%% Function Call
[X,t_IF, t_FS, zVals] = changesN_TestChaosMain( N, G, alphaD, omegaD, xD, NumberWindows, WindowLength, dt);
lifetimes(kk) = t_FS - t_IF;
I got the error below:
Error using rng (line 133)
First input must be a nonnegative integer seed less than 2^32,
'shuffle', 'default', or generator settings captured previously
using S = RNG.
Error in changesN_testcall1 (line 92)
rng(rstates{4});
Samson
Samson 2020 年 7 月 9 日
Hello Walter, thank you so much. You were so helpful. I used the following code and it gave me what I needed. However, in case of having the same random numbers, use rng('shuffle'). I have two questions now>
  • In running iteration #40 for instance with same initial conditions, what should I do precisely use the same initial conditions from my code below. I was thinking I have to put the initial values of #40 into the function and run the simulation but do not know how to do it. Kindly show me please.
  • How the I plot the lifetimes of #40 using histogram. Should I say in the command window,
H= histogram(lifetimes(40))?
Iter = 100;
lifetimes = zeros(1,Iter);
for ii = 1: length(omegaDArr) % omega_D iterated
omegaD = omegaDArr(ii);
%%% Repeated Simulations of each pair (DeltaOmega, Epsilon)
xDR = zeros(N,Iter);
for kk = 1 : Iter
xD = rand(N,1)*2*pi; % Init Cond. Driver
xDR(:,kk) = xD ;
%%% Function Call
[X, t_IF, t_FS, zVals] = changesN_TestChaosMain( N, G, alphaD, omegaD, xD, NumberWindows, WindowLength, dt);
end

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

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by