function [x_final]=random_walk(mu, sigma, num_steps, plot_flag)
% random_walk.m
%
% USAGE: [x_final]=random_walk(mu, sigma, num_steps, plot_flag)
%
% Simulate a random walk for num_steps number of steps.
% Each step is drawn from a normal distribution with mean mu and std sigma.
% Inputs:
%   mu        - mean of step dist.
%   sigma     - std of step dist.
%   num_step  - total number of step walker takes.
%   plot_flag - (optional) value of 0 results in no plot being created. 
%               Other values create figure of walker trajectory. 
%
% Outputs:
%   x - distance from origin after taking num_steps. 

% set default value for plot_flag to be 0 => no plot is created
if nargin <4, plot_flag = 0; end

% simulate walk
x = zeros(num_steps+1,1); %init vector of walker locations.
for i=1:num_steps
    x(i+1) = x(i) + normrnd(mu,sigma); % take random step
end
x_final = x(num_steps+1);

if plot_flag
    figure;
    t = [0,1:num_steps];
    plot(t,x);
    xlabel('# steps []');
    ylabel('Distance from start [au]');
    ylabel('Distance from start [au]');
    title('1-D Random Walk');
end
end