How do you set rng once and for all so that it is restored everytime random numbers needs to be generated?
Show older comments
Is there a way to avoid having to save the settings of the random generator (e.g. s = rng) and then restore the settings (e.g. rng(s)) every time I need to regenerate the same random sequence of numbers?
You may wonder why such a need. In a complex program, for example, which calls many functions which call many other functions, it is hard to know which line of code needs to be preceded by a rng(S) statement if I want to get the same results at the end.
Is there a possibility to have a global rng or whatever would avoid having to repeat to restore the settings?
Thanks
5 Comments
Walter Roberson
on 18 Sep 2018
Ummm. So if the code calls
A = rand(1,1);
B = rand(1,1);
C = rand(1,2);
you want A and B to be the same value, and you want C(1) to be the same as A and B, but C(2) to be different ?
Giovanni Barbarossa
on 18 Sep 2018
Edited: Giovanni Barbarossa
on 18 Sep 2018
Walter Roberson
on 18 Sep 2018
So every time rand is called it is to generate the same random number? Or should it generate two different values in the main routine but should give the first one again when called in the function?
Giovanni Barbarossa
on 20 Sep 2018
Walter Roberson
on 20 Sep 2018
To do that, you would call rng() before starting the classification learner. The classification learner cannot work properly if you return the same random value every time.
Accepted Answer
More Answers (2)
Jan
on 18 Sep 2018
No, this would be extremely confusing and not the nature of a "random number". If you want a function which behaves completely different than what rand is expected to, create your own function for this reason.
function S = StaticRand(varargin)
persistent Pool
siz = cell2mat(varargin);
n = prod(siz);
nPool = numel(Pool);
if n > nPool
Pool = cat(1, Pool, rand(n - nPool, 1));
end
S = reshape(Pool(1:n), siz);
end
Now this function replies the same random numbers for each call.
I cannot imagine, that such a function is really useful. It seems to be much smarter to call rand directly and to share the output by using arguments for the called functions. Sharing random values by storing them persistently in another function, looks like obfuscating.
1 Comment
Steven Lord
on 18 Sep 2018
function y = dilbertRand(sz)
% http://dilbert.com/strip/2001-10-25
% Modified to return a number in (0, 1)
y = repmat(0.9, sz);
function y = xkcdRand(sz)
% https://xkcd.com/221/
% Modified to return a number in (0, 1)
y = repmat(0.4, sz);
But on a more serious note, it sounds like you're trying to keep a tight rein on the random number generator, perhaps too tight a rein. As Peter Perkins stated in this Answer, don't try to reset the random number generator's state too often.
Ideally, set the random number generator once before you run your main function. When you want to reproduce the results of that call, set the random number generator to the same state and rerun your main function. That's essentially the "global rng" about which you asked.
If you want two separate functions to operate with the same "random" number, consider having the first of those functions you call pass the random number it generates into the second function as an input rather than manipulating the random number generator to generate the same "random" number in the second function. You're going to need to pass some state information between the two functions, lest you make a change to one of the functions and not the other and cause them to get out of sync. [Those types of bugs can be annoying to try to track down.] While you could pass the state of the random number generator, it's probably going to be easier (and maybe faster) to just share the numbers.
Categories
Find more on MATLAB 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!