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.
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.