function value = i4_uni ( )

%*****************************************************************************80
%
%% i4_uni() generates a random positive integer.
%
%  Discussion:
%
%    This procedure returns a random integer following a uniform distribution
%    over (1, 2147483562) using the current generator.
%
%    The original name of this function was "random()", but this conflicts
%    with a standard library function name in C.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%     05 August
%
%  Author:
%
%    Original Pascal version by Pierre L'Ecuyer, Serge Cote.
%    MATLAB version by John Burkardt.
%
%  Reference:
%
%    Pierre LEcuyer, Serge Cote,
%    Implementing a Random Number Package with Splitting Facilities,
%    ACM Transactions on Mathematical Software,
%    Volume 17, Number 1, March 1991, pages 98-111.
%
%  Output:
%
%    integer VALUE, the random integer.
%
  a1 = 40014;
  a2 = 40692;
  m1 = 2147483563;
  m2 = 2147483399;
%
%  Check whether the package must be initialized.
%
  if ( ~ initialized_get ( ) )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'I4_UNI - Note:\n' );
    fprintf ( 1, '  Initializing RNGLIB package.\n' );
    initialize ( );
  end
%
%  Get the current generator index.
%
  g = cgn_get ( );
%
%  Retrieve the keys for the current generator.
%
  [ cg1, cg2 ] = cg_get ( g );
%
%  Update the keys.
%
  k = i4_division ( cg1, 53668 );
  cg1 = a1 * ( cg1 - k * 53668 ) - k * 12211;

  if ( cg1 < 0 )
    cg1 = cg1 + m1;
  end

  k = i4_division ( cg2, 52774 );
  cg2 = a2 * ( cg2 - k * 52774 ) - k * 3791;

  if ( cg2 < 0 )
    cg2 = cg2 + m2;
  end
%
%  Store the updated keys.
%
  cg_set ( g, cg1, cg2 );
%
%  Construct the random integer from the keys.
%
  z = cg1 - cg2;

  if ( z < 1 )
    z = z + m1 - 1;
  end
%
%  If the generator is in antithetic mode, we must reflect the value.
%
  value = antithetic_get ( );

  if ( value )
    z = m1 - z;
  end

  value = z;

  return
end
