% The first line sets up Matlab's random number generator.
rand('twister', sum(100*clock)); 

n = size(A, 1);

Trials = 5000; % Trials is the number of random surfers.
Steps = 4000; % Steps is the number of clicks our random surfers will make.

Count = zeros(n, 1); % Count will be used to store the number of surfers 
                    % on each page at the end of the simulation.
                    
Deg = sum(A); % Deg stores the number of links from each page.

B=A; % We make a copy of B to preserve A, in case we wish to use it 
     % for some other purpose.

% The next lines attempt to turn each column of the adjacency matrix into a
% probability vector, by dividing by the number of links leaving the 
% corresponding vertex. If a page does not link to any other page this would 
% lead to dividing by zero, so whenever this happens we keep the column of
% zeros.

% The next lines perform the simulation.

for i=1:n
    if (Deg(i) > 0)
        B(:, i)=B(:, i)./Deg(i);
    else 
        B(:, i)=0;
    end
end

for i=1:Trials
    Position = randi(n); 
                         % We start each surfer at a randomly chosen vertex.
                         % We use Position to record a surfer's location.
    for j=1:Steps
        % If there is a link leaving a verex then we update Position to
        % reflect a random choice of link to follow. Otherwise we do not
        % change Position and the surfer is stuck on the page where they are.
        
        if Deg(Position) > 0
            Position = ProbGen(B(:, Position));
        end
    end
     % Each time we compute Position of a surfer at the end of the simulation
     % we add one to the Count of surfers in that Position.
    Count(Position) = Count(Position)+1;
end

% To turn Count into a set of Probability distributions, we divide every  
% element in the Count matrix by Trials.


Count ./ Trials

