function forgetful_burglar ( trial_num )

%*****************************************************************************80
%
%% forgetful_burglar(): the forgetful burglar problem.
%
%  Discussion:
%
%    In a town of 201 homes, a burglar starts at home 101.
%    He randomly moves one or two homes left or right.
%    What is the typical number of moves he will make before revisiting a home?
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    24 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Digital Dice: Computational Solutions to Practical Probability Problems,
%    Princeton, 2008,
%    ISBN: 978-0-691-15821-1.
%
%  Input:
%
%    trial_num: the number of trials.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'forgetful_burglar():\n' );
  fprintf ( 1, '  In a town of 201 homes, a burglar starts at home 101.\n' );
  fprintf ( 1, '  He randomly moves one or two homes left or right.\n' );
  fprintf ( 1, '  What is the typical number of moves he will make before revisiting a home?\n' );

  duration = zeros ( 1, 50 );

  for loop = 1 : trial_num

    go = 1;
    here_you_are = 101;
    where_youve_been = zeros ( 1, 201 );;
    where_youve_been(here_you_are) = 1;

    steps = 0;
    while ( go == 1 )
      move = sign ( rand ( ) - 0.5 ) * ( floor ( 2 * rand ( ) ) + 1 );
      here_you_are = here_you_are + move;
      steps = steps + 1;
      if ( where_youve_been(here_you_are) == 1 )
        go = 0;
        duration(steps) = duration(steps) + 1;
      else
        where_youve_been(here_you_are) = 1;
      end
    end

  end

  duration = duration / trial_num;

  fprintf ( 1, '\n' );
  fprintf ( 1, '   K    Prob(K)\n' );
  fprintf ( 1, '\n' );
  for k = 1 : 10
    fprintf ( 1, '  %2d  %g\n', k, duration(k) );
  end
%
%  Graphics.
%
  clf ( );
  bar ( duration );
  grid ( 'on' );
  title ( 'Forgetful Burglar:' );
  xlabel ( 'Number of burglaries before repeat' );
  ylabel ( 'Probability' );
  filename = 'fb.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end
