function [ stakes, w, l ] = casino_simulation ( stakes, n, verbose )

%*****************************************************************************80
%
%% casino_simulation() models a casino betting game that seems like a winner.
%
%  Discussion:
%
%    A casino offers the following game:
%    Put your initial stakes S on the table.
%    For N plays in a row, flip a fair coin, and if the result is:
%    * HEADS: we add 20% to your current stakes;
%    * TAILS: we take 17% of your current stakes.
%
%    It seems like this game offers you roughly a 1.5% profit on
%    every coin toss, so that in the long run, you would expect to
%    win big.  Is that correct?
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    12 January 2023
%
%  Reference:
%
%    Bruce Boghosian,
%    The Inescapable Casino,
%    Scientific American,
%    November 2019, pages 70-77.
%
%  Input:
%
%    real stakes: the amount of money you begin with.
%
%    integer n: the number of times you play.
%
%    logical verbose: true if you want to see the results of each toss.
%
%  Output:
%
%    real stakes: the current amount of money you have.
%
%    integer w, l: the number of times you won and lost a coin toss.
%
  if ( verbose )
    fprintf ( 1, '\n' );
    fprintf ( 1, '   i   win  lose    stakes\n' );
    fprintf ( 1, '\n' );
  end
%
%  Initialize the random number generator using the current time.
%
  rng ( 'shuffle' );
%
%  Play n games.
%
  for i = 0 : n

    if ( i == 0 )
      w = 0;
      l = 0;
    else
      coin = randi ( [ 0, 1 ] );
      if ( coin == 0 ) 
        l = l + 1;
        stakes = 0.83 * stakes;
      else
        stakes = 1.2 * stakes;
        w = w + 1;
      end
    end

    if ( verbose )
      fprintf ( 1, '  %5d  %5d  %5d  %14.2f\n', i, w, l, stakes );
    end

  end

  return
end

