How do you set rng once and for all so that it is restored everytime random numbers needs to be generated?
13 views (last 30 days)
Show older comments
Giovanni Barbarossa
on 18 Sep 2018
Commented: Giovanni Barbarossa
on 22 Sep 2018
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 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
Walter Roberson
on 20 Sep 2018
function output = rand(varargin)
output = ones(varargin{:}) * 7; %lucky number 7!
Now make sure this is one of the very first things in your path.
3 Comments
Walter Roberson
on 20 Sep 2018
As long as whatever code that is generating random numbers calls upon rand() to do the generation, then MATLAB would find your rand() routine instead the built-in one, and so would always return 7 for every random number.
The varargin is to take care of any size and type information that the user might be passing in their rand() call.
This will almost certainly mess up calculations: a lot of programs rely upon each rand call returning a number that is independent of the other calls to rand. But it is what you asked for, that every call to rand will return the same value.
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.
0 Comments
See Also
Categories
Find more on Get Started with Statistics and Machine Learning Toolbox 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!