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

%*****************************************************************************80
%
%% draw simulates drawing balls from a set of black and white balls.
%
%  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.
%
%  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
%
%  Parameters:
%
%    Input, integer b, w, the number of black and white balls at the
%    beginning of the round.
%
%    Output, integer blackball, whiteball, the number of black and
%    white balls at the end of the round.
%
  if ( rand ( ) < b / ( w + b ) )
    fcolor = 1;
    b = b - 1;
  else
    fcolor = 0;
    w = w - 1;
  end

  keepgoing = true;
  while ( keepgoing )

    if ( fcolor == 1 )

      if ( rand ( ) < b / ( w + b ) )
        b = b - 1;
        if ( b == 0 )
          keepgoing = false;
        end
      else
        keepgoing = false;
      end

    else

      if ( rand ( ) < w / ( w + b ) )
        w = w - 1;
        if ( w == 0 )
          keepgoing = false;
        end
      else
        keepgoing = false;
      end

    end

  end

  blackball = b;
  whiteball = w;

  return
end
