function [N_a_in,Th,eta,delay] = aloha(min_traffic, max_traffic,N,points)
% Get ALOHA performance
% min_traffic = minimum traffic applied (packet/slot)
% max_traffic = maximum traffic applied (packet/slot)
% N = number of users in the system
% points = number of data points to be generated
% N_a_in = input traffic vector
% Th = throughput vector (packet/slot)
% eta = efficiency vector 
% delay = user delay vector


% First make sure that maximum traffic makes sense.
if max_traffic > N
    max_traffic = N;
end

for i=1:points
    increment = (max_traffic-min_traffic)/points;
    a(i) = (min_traffic+increment*(i-1))/N;
    b(i) = 1-a(i);
    N_a_in(i) = a(i)*N;
    % calculate arrival probability for each value of a:
    for j=1:N+1
        arrivals(i,j) = nchoosek(N,j-1)*a(i)^(j-1)*b(i)^(N-j+1);
    end
end

for i=1:points
    Th(i) = arrivals(i,1)*arrivals(i,2);
    x  = Th(i)/(N*a(i));
    y = 1- x;
    eta(i) = Th(i)/N_a_in(i);
    delay(i) = abs(y/x);
end