% File name: uniform_dist.m
% Generate random numbers having the Poisson
% distribution.
% Two plots are produced. One on left is the
% plot of numbers that result.
% One on right is a histogram.
	
n=1000; %number of samples produced
x=rand(1,n); %generate n uniformly distributed
% sample
mu = 5; % value for mu 
% Calculate Poison CDF
exp_mu = exp(-mu);
CDF(1) = exp_mu;
for k = 2:50
   p(k) = mu^k*exp_mu/factorial(k);
   CDF(k) = p(k)+CDF(k-1);
end
% Generate Poisson-distributed random numbers
% using the inversion method.
for i = 1:n
   temp=find(CDF >= x(i));
   if size(temp,2) == 0
      y(i) = 0;
   else
      y(i) = temp(1)-1;
   end
end
subplot(1,2,1)
% Plot the values of all the numbers generated
plot(y,'k');%plot the resulting data
box off
axis square 
xlabel('Sample index')
ylabel('Random number value')
% Plot a histogram of the data to view the pdf
%x = 0:0.1:1;
subplot(1,2,2)
hist(y)
box off
axis square 
xlabel('Bins')
ylabel('Bin count')
