A strong nonrandom pattern in Matlab default random number generator

Matlab contains three generators of pseudorandom numbers. According to the documentation of the function rand, the user may choose between them using commands
rand('state',s)
rand('seed',s)
rand('twister',s)
where s is the seed. If none of these commands is used in a session, rand('state',0) is assumed. It appears to be quite useful not to rely on the default and set 'twister'.

The following Matlab code demonstrates a problem if the user does not set any of the methods explicitly or sets it to 'state':

Z = rand(28,100000);
ind = Z(1,:) < 0.25;
scatter(Z(16,ind),Z(28,ind),'.');
The problem is that this code produces approximately the following output

.

For a correct generator, the points should be uniformly distributed in the unit square. If we denote the sequence of the numbers generated by rand as z(i), then the rows 1, 16 and 28 of matrix Z used in the plot are formed by the numbers z(i+1), z(i+16) and z(i+28), where i is a multiple of 28. The observed pattern is due to the fact that there is a strong dependence between these three numbers, if the triples with different values of i are considered as realizations of a vector of three different random variables. If the index i is restricted to an arithmetic progression with step 28, then the triples are nonoverlapping and simulate independent realizations of the vector. However, the distribution of the triples does not change, even if we do not restrict the range of i and consider also overlapping triples.

An explanation of this dependence may be found using the source code of the generator. It is also possible to reproduce the dependence using an explicitly given transformation of any other random number generator. Examples of Monte Carlo integration which yield biased results due to this dependence allow to measure the level of the dependence quantitatively. See preliminary version of the paper on this topic for more detail.

A script demonstrating prediction of the magnitude of the next random nuber is predict.m.

A related problem, caused by the same property of the default setting of rand, is described at Technical Solutions, Solution Number: 1-10HYAS.

An exact description of the default generator is at Technical Solutions, Solution Number: 1-169M3.