function onedwalk ( )

%*****************************************************************************80
%
%% ONEDWALK models a random walk in one dimension.
%
%  Discussion:
%
%    This m-file simulates the one-dimensional, symmetrical walk
%    with an absorbing barrier at x = 3.
%    How long is the average walk?
%
%    1 <--> 3 <--> 4 --> 2
%
%  Modified:
%
%    29 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'ONEDWALK\n' );
  fprintf ( 1, '  This m-file simulates the one-dimensional, symmetrical walk\n' );
  fprintf ( 1, '  with an absorbing barrier at the right.\n' );
  fprintf ( 1, '  The map and connections:\n' );
  fprintf ( 1, '    1 <--> 3 <--> 4 --> 2\n' );
  fprintf ( 1, '  We start at location 1, and will stop at location 2.\n' );
  fprintf ( 1, '  How long is the average walk?\n' );

  web1 = [1;0;2;2];
  web2 = [3;0;1;2];
  web3 = [0;0;4;3];

  web = [web1 web2 web3];

  d = zeros(1,100);

  for walks = 1 : 10000
    position = 1;
    steps = 0;

    while ( position ~= 2 )
      choices = web(position,1);
      choice = floor ( choices * rand ( ) ) + 2;
      position = web(position,choice);
      steps = steps + 1;
    end
    d(steps) = d(steps) + 1;
  end

  average = 0;
  for i = 1 : 50
    average = average + i * d(i);
  end

  average = average / 10000;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Average walk duration = %g\n', average );
%
%  Graphics.
%
  clf
  plot ( d )
  title('Distribution of Walk Durations')
  xlabel('number of steps')
  ylabel('number of walks')
  print ( '-dpng', 'onedwalk.png' )

  return
end

