function p = twins ( )

%*****************************************************************************80
%
%% twins analyzes the twins problem.
%
%  Discussion:
%
%    There are 20 students in a class.  Two of the students are twins.
%    The class is divided into five lab groups of size four.
%
%    Estimate that probability that the twins are assigned to the same
%    lab group.
%
%  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:
%
%    Output, real P.
%    The expected value is 3/19.
%
  trial_num = 1000000;

  together = 0;

  for loop1 = 1 : trial_num

    lab = ones(1,20);
    twin1 = floor ( 20 * rand ( ) ) + 1;
    twin2 = twin1;
    while ( twin1 == twin2 )
      twin2 = floor ( 20 * rand ( ) ) + 1;
    end
    lab(twin1) = 2;
    lab(twin2) = 2;

    groupsum = zeros ( 1, 5 );
    for loop2 = 1 : 5
      x = 4 * ( loop2 - 1 );
      for loop3 = 1 : 4
        groupsum(loop2) = groupsum(loop2) + lab(x+loop3);
      end
    end

    for loop4 = 1 : 5
      if ( groupsum(loop4) == 6 )
        together = together + 1;
      end
    end

  end

  p = together / trial_num;

  return
end
