function [K,R] = mcmm1(lambda,mu,N,I,K0)
% Call [K,R] = mcmm1(lambda,mu,N,I,K0)
%Where A = offered traffic in Erlang (for M/x queue lambda = A/h)
%      k = holding time is seconds   (For M/x queye mu =1/h)
%      N = Maximum queue length
%      I = number of attempts/arrivals to process
%      K0 = optional staring State, Defaults to integer below A
%      K = sequence of states visited
%      R = sequence of random mumbers used

if (nargin == 4)
  K0 = 0; 
endif

% Initiatise rate 
s = ones(1,N+1)*lambda;
e = ones(1,N+1)*mu;
%
k=K0+1; % corresponding to starting state 
i=1; % iteration counter
 
do 
  r=rand(1,1);
  if (r < e(k))
    k = max(1,k-1);
  elseif (r < e(k)+s(k))
    k = min(N+1,k+1);
  endif     % Do nothing case is implicit
  K(i) = k-1;
   R(i) = r;
  i=i+1;
until (i>I)

