Clear Filters
Clear Filters

Random number not repeatable with the same seed

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

Answers (1)

Alessandro Masullo
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')?
Etsuko on 25 Apr 2016
Hi Alessandro, I found the line where this problem happens. I added two rand(1) in many places in my code, and found that the number from rand(1) placed after randg can be different between runs although the random seed and all other parameters are exactly the same. Strangely, when it gives inconsistent random numbers, it is not any number but from a small set of different numbers. For example, rand(1) placed after randg gave either 0.2498, 0.2393, or 0.0153 from many test runs. I suspect that there is something like numerical errors in randg function. I tried to create a script where I can demonstrate this is happening;
seedi = 984; sizeOut = [1,556]; WIJ = zeros(556,1); WIJ(11) = 0.4836; WIJ(36) = 0.3616; WIJ(end) = 1-sum(WIJ);
s = RandStream('mt19937ar','Seed',seedi); %randi(2^20) or 1 (=seedi) for fixed seed RandStream.setGlobalStream(s);
for i = 1:2:1000000 test(i)=rand(1); r = randg(400*WIJ',sizeOut); test(i+1)=rand(1); end
s = RandStream('mt19937ar','Seed',seedi); %randi(2^20) or 1 (=seedi) for fixed seed RandStream.setGlobalStream(s);
for i = 1:2:1000000 test2(i)=rand(1); r = randg(400*WIJ',sizeOut); test2(i+1)=rand(1); end
But the last statement is always 0 so far. Hence, I have not been able to convincingly demonstrate the phenomenon I see in my own code. It is very strange. It is undeniable that the problem lies in randg because rand(1) placed right after randg becomes inconsistent, but not the one right before randg. Do you have any clue as to what might be going on? I will also ask the forum if anyone has seen any similar issues with randg.
Thank you.
Alessandro Masullo
Alessandro Masullo on 26 Apr 2016
Hi Etsuko,
I don't think there's any problem with the rng function. Maybe, you're setting some additional options in your code to generate different sizes of sets of random numbers. I remember that I had a similar problem with a Monte Carlo simulation, and in my case, the issue was related to my script.
I give you a very basic example; imagine that you have an iterative script that runs on random numbers:
for i = 1:N_iter
input = rand(10);
output = sum(input(:));
Now, imagine that you want to compare two results of two different analysis, but one is based on N_iter = 3, the other on N_iter = 5. Since the rng is set at the beginning of the code, you won't be able to have a reliable comparison because the process of generating number is different (more iterations, in this case). Maybe in your case there's something similar.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!