Random number not repeatable with the same seed
16 views (last 30 days)
Hi, I have a lengthy Matlab script for a stochastic simulation model. Every time I run a simulation, I initialize the random number generator at the top of the code by using
s = RandStream('mt19937ar','Seed',seedi);
I expect that I should get the same results as long as the value of seedi is the same, but it is not. I set the number of CPUs to 1
LASTN = maxNumCompThreads(1)
to avoid possible complications due to using multiple CPUs. But this did not solve the problem. Why is this happening? I also tried rng(seedi, 'twister') but it did not solve the problem, either. The script uses many matlab functions that use random numbers (randsample, gamrnd, poisoned, etc) for very numerous times. Is there any possibility that these functions might be resetting the seed somehow in the background? If so, how can I stop it? I need a consistent stream of random numbers for debugging.
Alessandro Masullo on 22 Apr 2016
Edited: Alessandro Masullo on 22 Apr 2016
The number of threads shouldn't affect your script, because matlab only use it internally. If you don't use a parfor or some tools of the parallel toolbox, you can safely leave the number of threads.
For getting always the same random number, using rng should be enough. Where is your "seedi" coming from? It may happen that something in your code is changing the rng, although none of the standard matlab functions do it, as far as I know.
Where did you set rng(seed,'twister')?