%Number of time steps
time_steps=500;

%Number of initial types
s_types=100;

%Matrix containing the whole evolution of the system
evolution=zeros(time_steps,s_types);

%Matrix containing the whole evolution of the system counting the members
%of each species
evolution_c=zeros(time_steps,s_types);

%Vector used to store the update of a single time step
single_ev=zeros(1,s_types);

%The first row of the evolution vector contains all the types
evolution(1,:)=[1:1:s_types];

%The first row of the evolution_c vector has all the species
evolution_c(1,:)=1;

%Variable used to record the first at which the first and last type are the
%same
stat_t=0; flag=0;

%The counter of the loop starts from time=2 since time=1 has been used to
%record the initial situation
i=2;
while i<=time_steps
    j=1;
    %The number of slots equals the numer of types. Of course the available
    %variety of types diminish after each time step but the number of slots
    %remains the same.
    while j<=s_types 
        rn=randi([1,s_types]); %Generates a random number to pick the slot
        single_ev(1,j)=evolution(i-1,rn);
        j=j+1;
    end
    
    evolution(i,:)=sort(single_ev); %The single_ev is sorted to have a nice picture
    
    %Counts number of slots having a certain type at each step
    k=1;
    while k<=s_types
        l=1;
        while l<=s_types
            if evolution(i,l)==k
                evolution_c(i,k)=evolution_c(i,k)+1;
            end
            l=l+1;
        end
        k=k+1;
    end
    
    %Records the first time all the types are the same. We just need to
    %check the first and the last site because we order the vector at each
    %step.
    if evolution(i,1)==evolution(i,s_types)&&flag==0  
        stat_t=i;
        flag=1;
    end
    
    i=i+1;
end

figure
imagesc(evolution);
hold on;
xlabel('Types');
ylabel('Time');
colormap jet; %colorcube
colorbar;
axis('xy'); %This helps with y axis inversion
hold off;

figure
plot([1:1:time_steps],evolution(:,1));

figure
plot([1:1:time_steps],evolution_c(:,1));
