function p = before ( prob )

%*****************************************************************************80
%
%% before analyzes the second Newton gambling problem.
%
%  Discussion:
%
%    What is the probability of getting 4 heads before 7 tails?
%
%  Modified:
%
%    07 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, real prob, the probability of heads on a single toss.
%
%    Output, real P, the probability of getting 4 heads before 7 tails,
%    for which the exact value is:
%    0.733962, if p = 0.45
%    0.828125, if p = 0.50
%
  trial_num = 10000000;

  total = 0;

  for loop = 1 : trial_num

    H = 0;
    T = 0;

    while ( H < 4 && T < 7 )
      if ( rand() < prob )
        H = H + 1;
      else
        T = T + 1;
      end
    end

    if ( H == 4 )
      total = total + 1;
    end

  end

  p = total / trial_num;

  return
end
