function [Xout,Pout] = unifSample(X,p) % p: Probability array % X: State space u=rand; % Assign random number between 0 and 1 to the variable u P=cumsum(p); % Create a CMF from the PMF % Initialize the index variable, j. The possible values of j will % be the integers 1 through Length(p) j=1; while (u>P(j)) j=j+1; end Xout=X(j); % Xout becomes an entry in the multinomial random vector Pout=P(j); % Pout is the corresponding probability of Xout