How do you set rng once and for all so that it is restored everytime random numbers needs to be generated?

9 vues (au cours des 30 derniers jours)
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 commentaires
Giovanni Barbarossa
Giovanni Barbarossa le 20 Sep 2018
Same random number every time rand is called. You may find the question silly...for example I could simply store some random numbers in a variable etc. There are many solutions. However, if I am using a third party code that calls several functions which generate random numbers several times, let alone a Matlab function that calls other functions that generate random numbers, it is hard for me to find where I have to restore the random settings before every time rand is called.
For example, let's say I want to generate repeatable results with the Classification Learner so I can compare its results to my own code. How do I do that?
Walter Roberson
Walter Roberson le 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.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 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 commentaires
Walter Roberson
Walter Roberson le 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.

Connectez-vous pour commenter.

Plus de réponses (2)

Jan
Jan le 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 commentaire
Stephen23
Stephen23 le 18 Sep 2018
Modifié(e) : Stephen23 le 18 Sep 2018
+1 passing values is the way to go. Calling a non-random random-number generator is highly misleading code.

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 18 Sep 2018
On a not-so-serious note, replace your rand call with dilbertRand or xkcdRand.
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.

Catégories

En savoir plus sur Get Started with Statistics and Machine Learning Toolbox dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by