function [y]=poirv(n,lamb) 

%simulate a sample of Poisson random variable with mean lamb using the discrete 
%version of the inverse transform method 
% n is the number of random variables that are drawn from this distribution.

y=zeros(1,n);
for i=1:n, 
   p=exp(-lamb);
   j=0;
   F=p;
   u=rand(1);
   while( u>F),
      j=j+1;
      p=lamb*p/j; F=F+p; 
	end;
	y(i)=j;
end;
