function boom ( total )

%*****************************************************************************80
%
%% BOOM simulates the likelihood of a given number of sons in a family.
%
%  Discussion:
%
%    There are given probabilities of a man having 0, 1, 2, 3, 4, 5, 6 or 7 sons.
%    What are the chances of having 2 or 4 sons in the second generation?
%    Of having 6 sons in the third generation?
%
%  Modified:
%
%    27 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Digital Dice: Computational Solutions to Practical Probability Problems,
%    Princeton, 2008,
%    ISBN: 978-0-691-15821-1.
%
%  Parameters:
%
%    Input, integer TOTAL, the number of simulations to be done.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'BOOM:\n' );
  fprintf ( 1, '  There are given probabilities of a man having 0, 1, 2, 3, 4, 5, 6 or 7 sons.\n' );
  fprintf ( 1, '  What are the chances of having 2 or 4 sons in the second generation?\n' );
  fprintf ( 1, '  Of having 6 sons in the third generation?\n' );

  p0 = 0.4825;
  p1 = 0.2126;
  p2 = p1 * 0.5893;
  p3 = p2 * 0.5893;
  p4 = p3 * 0.5893;
  p5 = p4 * 0.5893;
  p6 = p5 * 0.5893;

  zero = p0;
  one = zero + p1;
  two = one + p2;
  three = two + p3;
  four = three + p4;
  five = four + p5;
  six = five + p6;

  answer = zeros ( 1, 3 );

  for loop = 1 : total

    gen2 = zeros ( 1, 7 );
    gen3 = zeros ( 1, 49 );
    gen1 = offspring ( zero, one, two, three, four, five, six );
    for loop1 = 1 : gen1
      gen2(loop1) = offspring ( zero, one, two, three, four, five, six );
    end
    index = 1;
    for loop2 = 1 : gen1
      for loop3 = 1 : gen2(loop2)
        gen3(index) = offspring ( zero, one, two, three, four, five, six );
        index = index + 1;
      end
    end
    n = sum ( gen2 );
    if ( n == 2 )
      answer(1) = answer(1) + 1;
    elseif ( n == 4 )
      answer(2) = answer(2) + 1;
    end

    if ( sum ( gen3 ) == 6 )
      answer(3) = answer(3) + 1;
    end
  end

  answer = answer / total;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Estimated probabilities:\n' );
  fprintf ( 1, '  2 males in generation 2 = %g\n', answer(1) );
  fprintf ( 1, '  4 males in generation 2 = %g\n', answer(2) );
  fprintf ( 1, '  6 males in generation 3 = %g\n', answer(3) );

  return
end
