How do I set a seed to generate different random initial numbers and storing them
Afficher commentaires plus anciens
for kk = 1 : Iter
xD = rand(N,1)*2*pi; % Init Cond. Driver
end
3 commentaires
Samson
le 2 Juil 2020
Wiley Mosley
le 3 Juil 2020
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
le 3 Juil 2020
Réponse acceptée
Plus de réponses (1)
Wiley Mosley
le 3 Juil 2020
Modifié(e) : Wiley Mosley
le 3 Juil 2020
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
Samson
le 3 Juil 2020
Samson
le 4 Juil 2020
Walter Roberson
le 4 Juil 2020
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
Samson
le 6 Juil 2020
Walter Roberson
le 6 Juil 2020
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.
Samson
le 7 Juil 2020
Modifié(e) : Walter Roberson
le 8 Juil 2020
Walter Roberson
le 8 Juil 2020
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
Samson
le 8 Juil 2020
Samson
le 9 Juil 2020
Catégories
En savoir plus sur MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!