function A = i4mat_perm_random ( A )

%*****************************************************************************80
%
%% i4mat_perm_random() selects a random permutation of an I4MAT.
%
%  Discussion:
%
%    The matrix is assumed to be square.  A single permutation is
%    applied to both rows and columns.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    12 November 2014
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    A Nijenhuis, Herbert Wilf,
%    Combinatorial Algorithms,
%    Academic Press, 1978, second edition,
%    ISBN 0-12-519260-6.
%
%  Input:
%
%    integer A(N,N), the array to be permuted.
%
%  Output:
%
%    integer A(N,N), the permuted array.
%
  n = size ( A, 1 );
%
%  Permute the rows and columns together.
%
  for i = 1 : n - 1

    i2 = randi ( [ i, n ], 1 );

    temp(1,1:n) = A(i2,1:n);
    A(i2,1:n) = A(i,1:n);
    A(i,1:n) = temp(1,1:n);

    temp(1:n,1) = A(1:n,i2);
    A(1:n,i2) = a(1:n,i);
    A(1:n,i) = temp(1:n,1);

  end

  return
end
