Search code examples
matlablcg

Output Argument Not Assigned During Call: Matlab


I am attempting to generate 2500 psuedo-random numbers using LCG for a project. However, when I attempt to run the code I continuously receive the error "Output argument 'p' (and maybe others) not assigned during call to lcgg'.". I was hoping someone could help me understand why p is not in the output and how I can fix this?

clear;
clc;

M = 2500;
ID = 801201076;
disp('N = '); disp(mod(ID,3));

[A,p1] = lcgg(M,30269,171,0,1);
[B,p2] = lcgg(M,30307,172,0,1);
[C,p3] = lcgg(M,30323,170,0,1);

disp('Period = '); disp(p2);

% Combine the 3 generators as in Wichmann and Hill

figure(1);
subplot(2,1,1);hist(B);title('Histogram for Uniform RDN from LCG');
subplot(2,1,2);qqplot(rand(300,1),B);title('QQplot for uniform RDN from LCG');

figure(2);
scatter(B(1:(M-1),1),B(2:M,1),4);title('Plot of sequential pairs for LCG');

D = A + B + C - fix(A + B + C); % internal Matlab uniform random number generator

u = rand(M,1); % internal Matlab uniform random number generator

figure(3);
subplot(2,1,1);scatter(u(1:(M-1),1),u(2:M,1),4);title('Plot of Sequential Pairs for Matlab Internal Generator');
subplot(2,1,2);scatter(D(1:M-1),1),D(2:M,1),4;title('Plot of sequential pairs for 3 LCG Combined')



% Calculate the period
i = 1;
j = 2;
while A(i,1) ~= A(j,1) && i < m
    if j < m
        j = j+1;
    else
        i = i+1;
        j = j+1;
    end
end
if i == m
    p = m;
else
    p = j-1;
end
A = A./m;
if M <= m
    A = A(1:M,:);
end

function[A,p] = lcgg(M,m,a,c,x0)
% Generates a matrix of random numbers using lcg
% Calculate the period
% Input: M: total number of random numbers needed
%         m, a, x, x0
% Output: A: M * 1 matrix of random numbers
%         p: period of the LCG random number generator
    A = zeros(m,1);
    for i = 1:m
        if i == 1
            A(i,1) = lcg(m,a,c,x0);
        else
            A(i,1) = lcg(m,a,c,A(i-1,1));
        end
    end 
end

% The LCG Function:

function[x] = lcg(m,a,c,x0)
    % Linear Congruential Generator (LCG)
    x = mod(a*x0+c, m);
end

Solution

  • You define a function:

    function[A,p] = lcgg(...)
    

    Within the function body you need to assign a value to both output variables, A and p. You don’t assign anything to p, hence the message.