function x = randd(pmf,n)
%randd(pmf,n) produces a 1xn array of pseudo-random integers with 
%probabilities specified in the vector pmf.  If n is absent a single 
%such vale is produced.
%
if nargin==1
	n=1;
end
c=cumsum(pmf);
pick=@(u) find(u<=c,1);			%Anonymous function for first i with u<=c(i).
x=arrayfun(pick,rand([1,n]));	%Apply to each entry of i.i.d. unform array.
end
% M. Day, August 7, 2014