function [y, seq] = walk1(n)
%WALK1 Random walk in 1-dimension
%  Y = WALK1(N) returns the last value in a 1-dimensional
%  random walk after N steps.
%
%  [Y, SEQ] = WALK1(N) also returns the sequence of N values
%  along the random walk.
%
%  The number of steps is optional. WALK1() uses N=100 steps.

if ~exist('n')
    n = 100;
end

seq = zeros(1, n);
for t = 2:n
    flip = rand(1);
    if flip < 0.5 
        seq(t) = seq(t - 1) + 1;
    else
        seq(t) = seq(t - 1) - 1;
    end
end
y = seq(end);
