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

for kk = 1 : Iter
xD = rand(N,1)*2*pi; % Init Cond. Driver
end

3 commentaires

I need to store up the different random for each realisation.
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');
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

Connectez-vous pour commenter.

 Réponse acceptée

Plus de réponses (1)

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 commentaires

Hello, I appreciate your response deeply. I do not want repeated initial conditions. I discovered that the random number something do not produce random numbers. I need to run 100 realization with 100 different initial conditions. xD = 50 oscillators that would need 50 initial conditions and run for 100 realizations.
Hello Wiley, please how do I save the randon numbers. Your method gave me different random number on display in the workspace but I need to save them so I can retrieve foe any realization. Thank you so much
rng(1,'twister'); % init generator for random repeatable with seed 1
s = rng; % save generator settings as s
xD = cell(Iter,1);
for kk = 1: Iter
xD{kk} = rand (N, 1) * 2 * pi; % Init Cond. Driver
end
rng(s) % Reset the generator
newxD = cell(Iter,1);
for kk = 1: Iter
newxD{kk} = rand (N, 1) * 2 * pi; % Init Cond. Driver
end
is_the_same = cellfun(@(First,Second) isequal(First,Second), xD, newxD);
where_are_differences = find(~is_the_same);
if isempty(where_are_differences)
fprintf('All iterations match exactly!\n')
else
fprintf('%d iterations do not match exactly!\n', length(where_are_differences));
fd = where_are_differences(1);
fprintf('First one is at iteration %d\n', fd);
fprintf('The first run produced:\n')
disp(xD{fd});
fprintf('\nThe second run produced:\n')
disp(newxD{fd});
fprintf('\n');
end
Hello,
your code gave me an error. asking me to define cell.
Also, sincerely do not understand this below:
is_the_same = cellfun(@(First,Second) isequal(First,Second), xD, newxD);
where_are_differences = find(~is_the_same);
if isempty(where_are_differences)
fprintf('All iterations match exactly!\n')
else
fprintf('%d iterations do not match exactly!\n', length(where_are_differences));
fd = where_are_differences(1);
fprintf('First one is at iteration %d\n', fd);
fprintf('The first run produced:\n')
disp(xD{fd});
fprintf('\nThe second run produced:\n')
disp(newxD{fd});
fprintf('\n');
end
Your posted code does not define Iter or N, so I did not define Iter or N in the code. If you define (for example)
Iter = 500;
N = 1000;
and then the code I posted, then it will execute. The result on my system is that all iterations match exactly -- which tells you that using rng() to find the current seed, and then using rng() to restore that seed, does indeed produce exactly the same results.
The code creates a bunch of random vectors each of length N, storing the vectors into the cell array xD. Then it changes the random number seed back to what was recorded, and generates a bunch more random vectors of length, storing the vectors into a cell array newxD.
Having created the two cell arrays, it uses cellfun to run isequal() between corresponding cells. The cellfun call is equivalent to
is_the_same = false(size(xd));
for K = 1 : numel(xd)
is_the_same(K) = isequal(xd{K}, newxD{K});
end
This compares bit-for-bit equality (well, except for the treatment of NaN.) If any of the corresponding elements of the vectors differ in even a single bit, then the result would be false for the two vectors.
The code then asks to find the locations of everywhere where the two cells were not equal. If there were no differences, if every cell was exactly equal to its partner, then the result of the find() will be empty, and in that case the "match exactly" message will be generated. Otherwise, if even a single vector did not match its partner, then the code will display the number of places the vectors were different, and then will pick out the first place there is a mismatch and display the two corresponding contents.
The claim is that when you use rng() to restore the random number generator, that the values will be exactly reproduced, and this code proves the claim to be true.
You had also asked "please how do I save the randon numbers" and this code does exactly that: it saves every random number generated for every iteration.
Thank you Walter for your elaborate response. Kindly see my code below:
iterations(iter)=100; Number of oscillators (N)=50
for kk = 1 : Iter
xD = rand(N,1)*2*pi; % Init Cond. Driver
[t_IF, t_FS, zVals] = changesN_TestChaosMain( N, G, alphaD, omegaD, xD, NumberWindows, WindowLength, dt);
lifetimes(kk) = t_FS - t_IF;
end
What I want is to run the simulation with random initial conditions and calculating the lifetime each for each iterations. Now, I want to save all the random initial conditions (100) for each of these iterations or realizations. That having (50,100). So I can closely look at some data am interested in my calling the random initial condition used to generate the particular lifetime
Pardon me, am new to Matlab, I believe I have explain myself
Iter = 100;
rstates = cell(Iter,1);
for kk = 1 : Iter
rstates{kk} = rng(); %record current rng
xD = rand(N,1)*2*pi; % Init Cond. Driver
[t_IF, t_FS, zVals] = changesN_TestChaosMain( N, G, alphaD, omegaD, xD, NumberWindows, WindowLength, dt);
lifetimes(kk) = t_FS - t_IF;
end
Later if you wwant to look at xD for iteration #17, then
rng(rstates{17});
xD = rand(N,1)*2*pi; % same random values used for iteration #17
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});
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Produits

Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by