function smoker ( )

%*****************************************************************************80
%
%% SMOKER considers the two matchbook problem.
%
%  Discussion:
%
%    A smoker buys two packs of 40 matches.
%    He then repeatedly selects a match from a randomly chosen pack.
%    When one pack runs out, how many matches have been used in total?
%
%  Modified:
%
%    23 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Digital Dice: Computational Solutions to Practical Probability Problems,
%    Princeton, 2008,
%    ISBN: 978-0-691-15821-1.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'SMOKER:\n' );
  fprintf ( 1, '  A smoker buys two packs of 40 matches.\n' );
  fprintf ( 1, '  He then repeatedly selects a match from a randomly chosen pack.\n' );
  fprintf ( 1, '  When one pack runs out, how many matches have been used in total?\n' );

  S = 0;
  matches = zeros ( 1, 80 );
  for k = 1 : 1000000
    booklet1 = 40;
    booklet2 = 40;
    while ( booklet1 > 0 && booklet2 > 0 )
      r = rand ( );
      if ( r < 0.5 )
        booklet1 = booklet1 - 1;
      else
        booklet2 = booklet2 - 1;
      end
    end

    s = ( 40 - booklet1 ) + ( 40 - booklet2 );
    S = S + s;
    matches(s) = matches(s) + 1;

  end

  S = S / 1000000;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Average total number of matches used = %g\n', S );
%
%  Graphics.
%
  clf
  bar ( matches );
  title ( 'SMOKER: The Smoker''s Matchbook Problem' )
  grid on;
  xlabel ( 'Number of matches removed from both booklets' );
  ylabel ( 'Number of simulations' );
  filename = 'smoker.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
  pause ( 5 );

  return
end
