%%
N = 4000;
nstep = 100000;
n = zeros(nstep,1);
n(1) = N; % Initial number on left side
for i = 2:nstep
    r = rand(1,1);
    if (r<n(i-1)/N)
        n(i) = n(i-1) - 1; % Move atom from left to right
    else
        n(i) = n(i-1) + 1; % Move atom from right to left
    end
end
figure(1), plot((1:nstep),n,'r',(1:nstep),N-n,'k')
xlabel('t'),ylabel('n')
legend('Particles in left side of box','Particles in right side of box')
