function p = black ( b_init, w_init )

%*****************************************************************************80
%
%% black() estimates the probability that the last ball drawn is black.
%
%  Discussion:
%
%    A bag of black and white marbles is to be emptied in a series of rounds.
%
%    A round begins when a single ball is drawn from the bag, its color is 
%    noted, and the ball is discarded.  Then, one at a time, more balls are 
%    drawn from the bag.  If they match the first ball, then they are also 
%    discarded.  Otherwise, they are replaced in the bag, and this round stops.
%
%    Rounds are taken until the bag is empty.
%
%    This function simulates the process of a single round.
%
%    It is desired to estimate the probability that the last marble in 
%    the bag is black.  It turns out this probability is 1/2.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    08 May 2019
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Will You Be Alive 10 Years From Now?,
%    Princeton, 2014,
%    ISBN: 978-0691156804,
%    LC: QA273.25.N344
%
%  Input:
%
%    integer b_init, w_init: the initial number of black and white
%    balls;  Typical values are b_init = 15, w_init = 39;
%
%  Output:
%
%    real p, the probability that the last ball is black.
%
  total = 0.0;
  trial_num = 100000;

  for loop = 1 : trial_num

    b = b_init;
    w = w_init;

    while ( 0 < b + w )

      [ blackball, whiteball ] = draw ( b, w );
      b = blackball;
      w = whiteball;

      if ( b == 0 )
        w = 0;
      elseif ( w == 0 )
        total = total + 1;
        b = 0;
      end

    end

  end

  p = total / trial_num;

  return
end
