jeudi 27 juillet 2017

Random number generator issues in matlab

I have an issue I can't quite figure out. For a simulation I generate artificial data randomly, with randomly drawn variance and a mean of 0. To acheive this I first create a vector of possible variances and then randomly draw the index for the vector, like in the following example

%% Covariance Matrix

% Variances of explanatory variables
var1 = 0.1:0.1:100;
var2 = 0.1:0.1:100;
var3 = 0.1:0.1:100;

%% Randomly selecting variances

% if exist('s','var')
%     rng(s) % Loading Random generator settings for replication
% else
%     s=rng; % Saving Random generator settings for replication
% end

ind_1=randi([0 1000]);
ind_2=randi([0 1000]);
ind_3=randi([0 1000]);

var_11=var1(ind_1);
var_22=var2(ind_2);
var_33=var3(ind_3);

For some reason the random number generator seems to give me the same numbers in the first (ind_1=815, ind_2=906, ind_3=127) and in the second run (ind_1=914, ind_2=632, ind_3=97) after restarting matlab, if I generate the vector of variances first. I've been able to replicate that on different PC as well. Is there a feature that I'm overlooking or am I making, and I would imagine I am, a crucial mistake? (I am well aware that there are only pseudo random numbers in matlab, but this seems too pseudo for my taste)

Thanks, Richard




Aucun commentaire:

Enregistrer un commentaire