function y = randbin(n,p)
%RANDBIN Binomial random number generator.
%	RANDBIN(N,P) generates a random deviate from the binomial
%	distribution with denominator n and success probability P.
%
%	See RAND.

% GKS 12 Aug 95
% Uses naive inversion method.

f = zeros(n+1,1);
for i=0:n, f(i+1) = binp(i,n,p); end
rand('uniform'); u = rand(1);
k = find( u < f );
y = k(1) - 1;
