function advance_state ( k )

%*****************************************************************************80
%
%% ADVANCE_STATE advances the state of the current generator.
%
%  Discussion:
%
%    This procedure advances the state of the current generator by 2^K
%    values and resets the initial seed to that value.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    30 March 2013
%
%  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.
%
%  Parameters:
%
%    Input, integer K, indicates that the generator is to be
%    advanced by 2^K values.
%    0 <= K.
%
  a1 = 40014;
  a2 = 40692;
  m1 = 2147483563;
  m2 = 2147483399;

  if ( k < 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'ADVANCE_STATE - Fatal error!'
    fprintf ( 1, '  Input exponent K is out of bounds.'
    error ( 'ADVANCE_STATE - Fatal error!' );
  end
%
%  Check whether the package must be initialized.
%
  if ( ~ initialized_get ( ) )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'ADVANCE_STATE - Note:\n' );
    fprintf ( 1, '  Initializing RNGLIB package.\n' );
    initialize ( );
  end
%
%  Get the current generator index.
%
  g = cgn_get ( );

  b1 = a1;
  b2 = a2;

  for i = 1 : k
    b1 = multmod ( b1, b1, m1 );
    b2 = multmod ( b2, b2, m2 );
  end

  [ cg1, cg2 ] = cg_get ( g );
  cg1 = multmod ( b1, cg1, m1 );
  cg2 = multmod ( b2, cg2, m2 );
  cg_set ( g, cg1, cg2 );

  return
end
