function A = graph_adj_random_edges ( node_num, edge_num )

%*****************************************************************************80
%
%% graph_adj_random_edges() generates a random graph on given nodes and edges.
%
%  Discussion:
%
%    Each edge will show up twice in the adjacency matrix.
%    The number of edges must be between 0 and (node_num*(node_num-1))/2.
%    A(I,J) is nonzero if there is an edge from node I to node J.
%    A(I,I) will always be 0.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    15 March 2023
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer node_num: the number of nodes.
%
%    integer edge_num: the number of edges.
%
%  Output:
%
%    integer A(node_num,node_num): the adjacency matrix.  
%

%
%  Check.
%
  if ( node_num <= 0  )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'graph_adj_random_edges(): Fatal error!\n' );
    fprintf ( 1, '  node_num = %d\n', node_num );
    fprintf ( 1, '  but node_num must be at least 1.\n' );
    error ( 'graph_adj_random_edges(): Fatal error!\n' );
  end

  edge_max = ( node_num * ( node_num - 1 ) ) / 2;

  if ( edge_num < 0 || edge_max < edge_num )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'graph_adj_random_edges(): Fatal error!\n' )';
    fprintf ( 1, '  edge_num = %d\n', edge_num );
    fprintf ( 1, '  but edge_num must be at least 0, and\n' );
    fprintf ( 1, '  no more than %d\n', edge_max );
    error ( 'graph_adj_random_edges(): Fatal error!\n' );
  end
%
%  Initialize the adjacency matrix.
%
  A = zeros ( node_num, node_num );
%
%  Pick IWORK, a random subset of edge_max, of size edge_num.
%
  subset = ksub_random ( edge_max, edge_num );
%
%  The usable spots in the superdiagonal are numbered as follows:
%
%  * 1  2   3  ...  n-1
%  * * n+1 n+2 ... 2n-3
%  ...
%  * *  *   *  ... (n*(n-1))/2
%  * *  *   *  ...   * 
%
  k = 0;
  l = 1;

  for i = 1 : node_num - 1
    for j = i + 1 : node_num

      k = k + 1;

      if ( l <= edge_num )

        if ( k == subset(l) )
          A(i,j) = 1;
          A(j,i) = 1;
          l = l + 1;
        end

      end

    end
  end

  return
end
