function rolls ( )

%*****************************************************************************80
%
%% rolls() simulates the toilet paper problem.
%
%  Discussion:
%
%    Two rolls of toilet paper are installed in a toilet, with 200 sheets.
%    There are two kinds of people, with probabilities p and 1-p.
%      big choosers take one sheet from the larger roll;
%      little choosers take one sheet from the smaller roll (unless empty).
%    When one roll becomes empty, how many sheets are on the other roll?
%
%  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.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'rolls():\n' );
  fprintf ( 1, '  Two rolls of toilet paper are installed in a toilet, with 200 sheets.\n' );
  fprintf ( 1, '  There are two kinds of people, with probabilities p and 1-p.\n' );
  fprintf ( 1, '  * big choosers take one sheet from the larger roll;\n' );
  fprintf ( 1, '  * little choosers take one sheet from the smaller roll (unless empty).\n' );
  fprintf ( 1, '  When one roll becomes empty, how many sheets are on the other roll?\n' );
  fprintf ( 1, '  Use graphics to display results.\n' );

  M = zeros( 200, 200 );
  prob = 0;

  for loop = 1 : 1000

    prob = prob + 1;
    p = prob / 1000;
    M(1,1) = 1;
    for m = 2 : 200
      M(m,1) = ( 1.0 - p ) * m + p * M(m-1,1);
    end
    M(2,2) = M(2,1);
    diagonal = 3;
    for m = 3 : 200
      for n = 2 : diagonal - 1
        M(m,n) = p * M(m-1,n) + ( 1.0 - p ) * M(m,n-1);
      end
      M(m,m) = M(m,m-1);
      diagonal = diagonal + 1;
    end
    curve(prob) = M(200,200);
  end 
%
%  Graphics.
%
  clf ( );
  p = 1 : 1000;
  p = p / 1000;
  plot ( p, curve, 'linewidth', 3 );
  grid ( 'on' );
  title ( 'The Toilet Paper Problem' );
  xlabel ( 'p' );
  ylabel ( 'M200(p)' );
  filename = 'rolls.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end

