function aandb ( trial_num )

%*****************************************************************************80
%
%% aandb(): Parrondo's paradox.
%
%  Discussion:
%
%    In game A, you flip a biased coin, which shows heads with probabiity]
%    1/2 - epsilon; you win a dollar on heads.
%    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.
%    Both games A and B are losing games for you.
%    But, paradoxically, if you randomly switch back and forth between
%    one game and the other, you end up winning over the long term.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    07 May 2019
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Digital Dice: Computational Solutions to Practical Probability Problems,
%    Princeton, 2008,
%    ISBN: 978-0-691-15821-1.
%
%  Input:
%
%    integer trial_num: the number of trials.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'aandb():\n' );
  fprintf ( 1, '  In game A, you flip a biased coin, which shows heads with probabiity]\n' );
  fprintf ( 1, '  1/2 - epsilon; you win a dollar on heads.\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, '  Both games A and B are losing games for you.\n' );
  fprintf ( 1, '  But, paradoxically, if you randomly switch back and forth between\n' );
  fprintf ( 1, '  one game and the other, you end up winning over the long term.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Use graphics to display the winnings.\n' );

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

  for loop = 1 : trial_num

    m = 0;
    sample_function = zeros ( 1, 100 );
    for flips = 1 : 100

      which_game = rand ( );

      if ( which_game < 0.5 )

        outcome = rand ( );
        if ( outcome < 0.5 - epsilon )
          m = m + 1;
        else
          m = m - 1;
        end

      else

        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

      end

      sample_function(flips) = m;

    end

    Mtotal = Mtotal + sample_function;

  end

  Mtotal = Mtotal / trial_num;
%
%  Graphics.
%
  clf ( );
  flips = 1 : 100;
  plot ( flips, Mtotal, 'linewidth', 3 );
  grid ( 'on' );
  title ( 'Parrondo''s A and B Paradox' );
  xlabel ( 'Number of coin flips' );
  ylabel ( 'Ensemble average of capital.' );
  filename = 'aandb.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end
