function [ xc, rank, seed ] = comp_random_grlex ( kc, rank1, rank2, seed )

%*****************************************************************************80
%
%% COMP_RANDOM_GRLEX: random composition with degree less than or equal to N.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    09 September 2014
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer KC, the number of parts in the composition.
%
%    Input, integer RANK1, RANK2, the minimum and maximum ranks.
%    1 <= RANK1 <= RANK2.
%
%    Input, integer SEED, the random number seed.
%
%    Output, integer XC(KC), the random composition.
%
%    Output, integer RANK, the rank of the composition.
%
%    Output, integer SEED, the updated random number seed.
%

%
%  Ensure that 1 <= KC.
%
  if ( kc < 1 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'COMP_RANDOM_GRLEX - Fatal error!\n' );
    fprintf ( 1, '  KC < 1\n' );
    error ( 'COMP_RANDOM_GRLEX - Fatal error!' );
  end
%
%  Ensure that 1 <= RANK1.
%
  if ( rank1 < 1 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'COMP_RANDOM_GRLEX - Fatal error!\n' );
    fprintf ( 1, '  RANK1 < 1\n' );
    error ( 'COMP_RANDOM_GRLEX - Fatal error!' );
  end
%
%  Ensure that RANK1 <= RANK2.
%
  if ( rank2 < rank1 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'COMP_RANDOM_GRLEX - Fatal error!\n' );
    fprintf ( 1, '  RANK2 < RANK1\n' );
    error ( 'COMP_RANDOM_GRLEX - Fatal error!' );
  end
%
%  Choose RANK between RANK1 and RANK2.
%
  [ rank, seed ] = i4_uniform_ab ( rank1, rank2, seed );
%
%  Recover the composition of given RANK.
%
  xc = comp_unrank_grlex ( kc, rank );

  return
end
