function r = randex(p,q)

% Multiplicative congruential 
% uniform random number generator
%
% r = randex
% generates a single random number.
%
% r = randex(m,n)
% generates an m-by-n random matrix.
% 
% clear randex
% reinitializes the generator.
%
% Seeds are set by editing this file.

persistent a c m x

if isempty(x)
   a = 13;
   c = 0;
   m = 31;
   x = 1;
end

if nargin < 1, p = 1; end

if nargin < 2, q = p; end

r = zeros(p,q); %Preallocate memory
for k = 1:p*q
   x = rem(a*x + c, m);
   r(k) = x/m;
end