function A = jordan_matrix_random ( n )

%*****************************************************************************80
%
%% jordan_matrix_random() returns a Jordan matrix with random eigenvalues.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    15 January 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N, the order of the matrix.
%
%  Output:
%
%    real A, the random Jordan matrix.
%
  if ( nargin < 1 )
    n = 10;
  end
%
%  Choose K, the number of distinct eigenvalues, biased towards N.
%
  k = randi ( [ 0, n^2 - 1 ] );
  k = floor ( sqrt ( k ) );
  k = k + 1;
%
%  Choose K nonzero multiplicities that sum to N.
%
  s = compnz_random ( n, k );
%
%  Choose K distinct real eigenvalues between -2*N and +2*N.
%
  ev = ksub_random ( n + 1, k );
  ev = sort ( ev );
  ev(1:k) = 4.0 * ( ev(1:k) - 1.0 ) - 2.0 * n;
%
%  Construct Jordan matrix.
%  With 80% probability, include a superdiagonal 1.
%
  A = zeros ( n, n );
  i = 0;
  for block = 1 : k
    for ii = 1 : s(block)
      i = i + 1;
      A(i,i) = ev(block);
      if ( ii < s(block) )
        if ( rand ( ) <= 0.80 )
          A(i,i+1) = 1.0;
        end
      end
    end
  end

  return
end

