How to set a unified seed for random number genera

2019-05-20 21:31发布

问题:

I'm writing code and using existing functions from MATLAB. What if these functions use random number generators. Is there a way I can fix the seed of these functions without having to change their code ? Is there a command in MATLAB that does this ?

回答1:

Normally code would use Matlab's built-in random number generator. You can seed it with the following:

rng         = RandStream.getDefaultStream;
rng.reset(your_seed_value_here);

One could also create RandStream objects and use them (thereby avoiding the default stream), but this should work most of the time.