13 views (last 30 days)

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

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.

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.

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.

Stephen Cobeldick
on 18 Sep 2018

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.

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/419494-how-do-you-set-rng-once-and-for-all-so-that-it-is-restored-everytime-random-numbers-needs-to-be-gene#comment_611547

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/419494-how-do-you-set-rng-once-and-for-all-so-that-it-is-restored-everytime-random-numbers-needs-to-be-gene#comment_611547

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/419494-how-do-you-set-rng-once-and-for-all-so-that-it-is-restored-everytime-random-numbers-needs-to-be-gene#comment_611726

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/419494-how-do-you-set-rng-once-and-for-all-so-that-it-is-restored-everytime-random-numbers-needs-to-be-gene#comment_611726

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/419494-how-do-you-set-rng-once-and-for-all-so-that-it-is-restored-everytime-random-numbers-needs-to-be-gene#comment_611737

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/419494-how-do-you-set-rng-once-and-for-all-so-that-it-is-restored-everytime-random-numbers-needs-to-be-gene#comment_611737

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/419494-how-do-you-set-rng-once-and-for-all-so-that-it-is-restored-everytime-random-numbers-needs-to-be-gene#comment_612383

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/419494-how-do-you-set-rng-once-and-for-all-so-that-it-is-restored-everytime-random-numbers-needs-to-be-gene#comment_612383

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/419494-how-do-you-set-rng-once-and-for-all-so-that-it-is-restored-everytime-random-numbers-needs-to-be-gene#comment_612384

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/419494-how-do-you-set-rng-once-and-for-all-so-that-it-is-restored-everytime-random-numbers-needs-to-be-gene#comment_612384

Sign in to comment.