function gameb ( )

%*****************************************************************************80
%
%% GAMEB: Game B of Parrondo's paradox.
%
%  Discussion:
%
%    In game B, you have two biased coins.  If, at the time just before you
%    decide to flip, your capital M is a multiple of 3 dollars, you chose
%    coin 1, which shows heads with probability 1/10 - epsilon, otherwise
%    you choose coin 2, which shows heads with probability 3/4 - epsilon.
%
%    Game B is a losing game for you, and this code simply demonstrates
%    that using many simulations.
%
%  Modified:
%
%    25 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, 'GAMEB:\n' );
  fprintf ( 1, '  In game B, you have two biased coins.  If, at the time just before you\n' );
  fprintf ( 1, '  decide to flip, your capital M is a multiple of 3 dollars, you chose\n' );
  fprintf ( 1, '  coin 1, which shows heads with probability 1/10 - epsilon, otherwise\n' );
  fprintf ( 1, '  you choose coin 2, which shows heads with probability 3/4 - epsilon.\n' );
  fprintf ( 1, '  Game B is a losing game for you, and this code simply demonstrates\n' );
  fprintf ( 1, '  that using many simulations.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Produce a plot showing how the player loses.\n' );

  epsilon = 0.005;
  Mtotal = zeros ( 1, 100 );

  for loop = 1 : 10000

    m = 0;
    sample_function = zeros ( 1, 100 );

    for flips = 1 : 100

      if ( rem ( m, 3 ) == 0 )
        outcome = rand ( );
        if ( outcome < 0.1 - epsilon )
          m = m + 1;
        else
          m = m - 1;
        end
      else
        outcome = rand ( );
        if ( outcome < 0.75 - epsilon )
          m = m + 1;
        else
          m = m - 1;
        end
      end

      sample_function(flips) = m;

    end

    Mtotal = Mtotal + sample_function;

  end

  Mtotal = Mtotal / 10000;
%
%  Graphics.
%
  clf
  flips = 1 : 100;
  plot ( flips, Mtotal, 'linewidth', 3 );
  grid on
  title ( 'GAMEB: Parrondo''s Paradox' );
  xlabel ( 'Number of coin flips' );
  ylabel ( 'Ensemble average of capital.' );
  filename = 'gameb.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
  pause ( 5 );
 
  return
end
