% The code will run a random walk on a one-dimensional lattice (lattice spacing: delta) taking
% one step in time tau. The code returns the time that the walker first
% returns to the origin

function [t]=first_return(delta,tau)

t=0;
h=0;
kill_switch=0; % this variable turns from 0 to 1 at the first return

%walk=[];

while kill_switch==0
%    walk=[walk;h];
    Random_number=rand(1);% generate a random number between zero and one
    
    if Random_number>.5
        h=h+delta; % when the number is greater than .5 take a step up
        t=t+tau; % record that time passed
    elseif Random_number<.5
        h=h-delta; % when the number is less than .5 take a step down 
        t=t+tau; % record that time passed  
    end
    
    
    if h==0;% h==0 % stop the while loop when the walker returns
        kill_switch=1;
    end
    
end
