function value = ignuin ( low, high )

%*****************************************************************************80
%
%% ignuin() generates a random integer in a given range.
%
%  Discussion:
%
%    Each deviate K satisfies LOW <= K <= HIGH.
%
%    If (HIGH-LOW) > 2,147,483,561, this procedure prints an error message
%    and stops the program.
%
%    IGNLGI generates integers between 1 and 2147483562.
%
%    MAXNUM is 1 less than the maximum generatable value.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    03 September 2018
%
%  Author:
%
%    Original FORTRAN77 version by Barry Brown, James Lovato.
%    MATLAB version by John Burkardt.
%
%  Input:
%
%    integer LOW, HIGH, the lower and upper bounds.
%
%  Output:
%
%    integer VALUE, a random deviate from
%    the distribution.
%
  maxnum = 2147483561;

  if ( high < low )
    fprintf ( 1, ' \n' );
    fprintf ( 1, 'IGNUIN - Fatal error!\n' );
    fprintf ( 1, '  HIGH < LOW.\n' );
    error ( 'IGNUIN - Fatal error!\n' );
  end

  width = high - low;

  if ( maxnum < width )
    fprintf ( 1, ' \n' );
    fprintf ( 1, 'IGNUIN - Fatal error!\n' );
    fprintf ( 1, '  Range HIGH-LOW is too large.\n' );
    error ( 'IGNUIN - Fatal error!\n' );
  end

  if ( low == high )
    value = low;
    return
  end

  ranp1 = width + 1;
  maxnow = ( maxnum / ranp1 ) * ranp1;

  while ( true )

    ign = i4_uni ( ) - 1;

    if ( ign <= maxnow )
      break
    end

  end

  value = low + mod ( ign, ranp1 );

  return
end
