function prob = bernoulli_dice ( )

%*****************************************************************************80
%
%% bernoulli_dice() simulates a Bernoulli dice problem.
%
%  Discussion:
%
%    This program models a two-player game.  Players A and B take turns
%    tossing a fair die.  The first to throw a 1 wins.  On the first
%    round, A gets 1 toss, then B gets 1 toss, on the second A gets 2
%    tosses, then B gets 2 tosses and so on.  
%
%    What is the probability that A will win?
%
%  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
%
%    James Bernoulii,
%    Acta Eruditorum,
%    1690.
%
%  Output:
%
%    real prob, the probability that player A will win.
%
  w = 0;
  p = 1.0 / 6.0;
  trial_num = 100000;

  for loop = 1 : trial_num
    
    a = 0;
    b = 0;
    turn = 1;
    keepgoing = true;

    while ( keepgoing )

      for loopa = 1 : turn
        if ( rand ( ) < p )
          a = 1;
        end
      end

      for loopa = 1 : turn
        if ( rand ( ) < p )
          b = 1;
        end
      end

      if ( a == 0 )
        if ( b == 0 )
          turn = turn + 1;
        else
          keepgoing = false;
        end
      else
        w = w + 1;
        keepgoing = false;
      end

    end

  end

  prob = w / trial_num;

  return
end
