function patrol ( trial_num, number )

%*****************************************************************************80
%
%% patrol() simulates the highway patrol car problem.
%
%  Discussion:
%
%    Consider a divided highway, and suppose it could be divided by
%    a grassy median or a concrete barrier.
%    Suppose NUMBER police cars patrol the highway.
%    Suppose an accident occurs at a randomly chosen location and lane.
%    Suppose all patrol cars immediately head towards the accident.
%    If a grassy median, then patrol cars in the wrong lane can 
%    immediately reverse direction.
%    For the concrete barrier, patrol cars in the wrong lane must
%    continue to the end of the highway and then turn around.
%    Estimate the average time required by a patrol car to reach the accident.
%
%  Example:
%
%    Number   Grass Concrete
%
%         1  0.3333   0.9997
%         2  0.2085   0.6671
%         3  0.1500   0.5005
%         4  0.1167   0.3999
%         5  0.0545   0.2001
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    28 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:
%
%    integer trial_num: the number of trials.
%
%    integer NUMBER, the number of patrol cars.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'patrol():\n' );
  fprintf ( 1, '  Consider a divided highway, and suppose it could be divided by\n' );
  fprintf ( 1, '  a grassy median or a concrete barrier.\n' );
  fprintf ( 1, '  Suppose NUMBER police cars patrol the highway.\n' );
  fprintf ( 1, '  Suppose an accident occurs at a randomly chosen location and lane.\n' );
  fprintf ( 1, '  Suppose all patrol cars immediately head towards the accident.\n' );
  fprintf ( 1, '  If a grassy median, then patrol cars in the wrong lane can \n' );
  fprintf ( 1, '  immediately reverse direction.\n' );
  fprintf ( 1, '  For the concrete barrier, patrol cars in the wrong lane must\n' );
  fprintf ( 1, '  continue to the end of the highway and then turn around.\n' );
  fprintf ( 1, '  Estimate the average time required by a patrol car to reach the accident.\n' );

  total_grass_distance = 0;
  total_concrete_distance = 0;

  for loop = 1 : trial_num

    gdist = zeros ( 1, number );
    cdist = zeros ( 1, number );

    if ( rand ( ) < 0.5 )
      acclane = 1;
    else
      acclane = 2;
    end

    x = rand ( );
    for k = 1 : number
      y(k) = rand ( );
      if ( rand ( ) < 0.5 )
        patlane(k) = 1;
      else
        patlane(k) = 2;
      end
    end

    for k = 1 : number

      if ( acclane == patlane(k) )

        if ( acclane == 1 )

          if ( y(k) < x )
            gdistance = x - y(k);
            cdistance = x - y(k);
          else
            gdistance = y(k) - x;
            cdistance = 2 + x - y(k);
          end

        else

          if ( x > y(k) )
            gdistance = x - y(k);
            cdistance = 2 - x + y(k);
          else
            gdistance = y(k) - x;
            cdistance = y(k) - x;
          end

        end

      else

        if ( patlane(k) == 1 )
          gdistance = abs ( x - y(k) );
          cdistance = 2 - x - y(k);
        else
          gdistance = abs ( x - y(k) );
          cdistance = x + y(k);
        end

      end

      gdist(k) = gdistance;
      cdist(k) = cdistance;

    end

    min_gdistance = min ( gdist );
    min_cdistance = min ( cdist );
    total_grass_distance = total_grass_distance + min_gdistance;
    total_concrete_distance = total_concrete_distance + min_cdistance;

  end

  total_grass_distance = total_grass_distance / trial_num;
  total_concrete_distance = total_concrete_distance / trial_num;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Average grass distance    = %g\n', total_grass_distance );
  fprintf ( 1, '  Average concrete distance = %g\n', total_concrete_distance );

  return
end
