function Symbols = RandomSymbols(N, Alphabet, Priors)
% RandomSymbols - generate a vector of random information symbols
%
% A vector of N random information symbols drawn from a given 
% alphabet and with specified a priori probabilities is produced.
%
% Inputs:
%   N        - number of symbols to be generated
%   Alphabet - vector containing permitted symbols
%   Priors   - a priori probabilities for symbols
%
% Example:
%   Symbols = RandomSymbols(N, Alphabet, Priors)

%% input checking:
%   Alphabet and Priors must be vectors of the same size
if ( min(size(Alphabet)) > 1 || min(size(Priors)) > 1 )
    error('Inputs Alphabet and Priors must be vectors.')
end

if ( length(Alphabet) ~= length(Priors) )
    error('Alphabet and Priors must have the same length.')
end

% a priori probabilities must add to 1, issue a warning and fix
% matters if they don't
if ( sum(Priors) ~= 1 )
    warning('A-priori probabilities must add to one. Fixed!')
    Priors = Priors/sum(Priors);
end

%% Algorithm:
%   compute cumulative sums over the vector priors (using function cumsum)
%   generate a length-N vector of uniform (b/w 0 and 1) random numbers
%   figure out where each of the random numbers falls in the cumulative sum
%   assign symbols from alphabet correspondingly

CPriors = [0 cumsum( Priors )];
rr = rand(1, N);

for kk=1:length(Alphabet)
    Matches = rr > CPriors(kk) & rr <= CPriors(kk+1);
    Symbols( Matches ) = Alphabet( kk );
end