how to close rng 'default' after using it once in a same program
7 views (last 30 days)
Show older comments
Suppose, there is a scenario where I am using random number generator(rand(n,1)) which I need to fix for the values of a variable for a loop. So, I am using the command rng 'default'. Again in the same program I need to vary the random numbers in some other loops. But for For this I need to stop rng 'default ' command. Can anybody tell me how to do this?
Thanks in advance.
2 Comments
Adam Danz
on 23 Sep 2019
Edited: Adam Danz
on 24 Sep 2019
I don't recommend setting rng('default') within a loop, if that's what you're doing. That defeats the purpose of using a random process since every iteration would have the same sequence of random numbers. If you need to keep track of the random number generator seed for each iteration of a loop, you can store it like this.
n = 5
rngState = [];
for i = 1:n
rngState = [rngState; rng()];
% Your code here
end
seeds = [rngState.Seed]
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!