function thief ( )

#*****************************************************************************80
#
## thief() simulates the Thief of Baghdad problem.
#
#  Discussion:
#
#    A thief is in a room with three doors.
#    He chooses one at random.
#    One door leads immediately to freedom.
#    Another leads back to the room, after S hours.
#    The third leads back to the room, after L hours.
#    If the thief is returned to the room, he randomly tries a door again.
#    How long is the average imprisonment?
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    08 February 2023
#
#  Author:
#
#    Original MATLAB version by Paul Nahin.
#    This version by John Burkardt.
#
#  Reference:
#
#    Paul Nahin,
#    Dueling Idiots and Other Probability Puzzlers,
#    Princeton, 2012,
#    ISBN: 978-0691155005.
#
  print ( '' )
  print ( 'thief()' )
  print ( '  A thief is in a room with three doors.' )
  print ( '  He chooses one at random.' )
  print ( '  One door leads immediately to freedom.' )
  print ( '  Another leads back to the room, after S hours.' )
  print ( '  The third leads back to the room, after L hours.' )
  print ( '  If the thief is returned to the room, he randomly tries a door again.' )
  print ( '  How long is the average imprisonment?' )

  long = 3
  short = 1
  total = 0
  p1 = 1 / 3
  p2 = 2 / 3

  for thiefn = 1 : 10000

    time = 0
    trytoescape = 1
    while ( trytoescape > 0 )

      door = rand ( )

      if ( door < p1 )
        total = total + time
        trytoescape = 0
      elseif ( door < p2 )
        time=time + short
      else
        time=time + long
      end

    end

  end

  total = total / 10000
  print ( '' )
  print ( '  Estimated average total time in prison = #g', total )
  print ( '  Exact value is #g', long + short )

  return
end
                                 
   
