function x = genmn ( parm )

%*****************************************************************************80
%
%% genmn() generates a multivariate normal deviate.
%
%  Discussion:
%
%    The method is:
%    1) Generate P independent standard normal deviates - Ei ~ N(0,1)
%    2) Using Cholesky decomposition find A so that A'*A = COVM
%    3) A' * E + MEANV ~ N(MEANV,COVM)
%
%    Note that PARM contains information needed to generate the
%    deviates, and is set up by SETGMN.
%
%    PARM(1) contains the size of the deviates, P
%    PARM(2:P+1) contains the mean vector.
%    PARM(P+2:P*(P+3)/2+1) contains the upper half of the Cholesky
%    decomposition of the covariance matrix.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    31 March 2013
%
%  Author:
%
%    Original FORTRAN77 version by Barry Brown, James Lovato.
%    MATLAB version by John Burkardt.
%
%  Input:
%
%    real PARM(P*(P+3)/2+1), parameters set by SETGMN.
%
%  Output:
%
%    real X(P), a random deviate from the distribution.
%
  p = floor ( parm(1) );
%
%  Generate P independent normal deviates.
%
  work = zeros ( p, 1 );
  for i = 1 : p
    work(i) = snorm ( );
  end
%
%  Compute X = MEANV + A' * WORK
%
  x = zeros ( p, 1 );

  for i = 1 : p
    icount = 0;
    ae = 0.0;
    for j = 1 : i
      icount = icount + j - 1;
      ae = ae + parm(i+(j-1)*p-icount+p+1) * work(j);
    end

    x(i) = ae + parm(i+1);

  end

  return
end
