function set_key ( cg1, cg2 )

%*****************************************************************************80
%
%% set_key() resets the initial key and state of the current generator.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    28 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.
%
%  Input:
%
%    integer CG1, CG2, the CG values for generator G.
%    1 <= CG1 < 2147483563
%    1 <= CG2 < 2147483399
%
  m1 = 2147483563;
  m2 = 2147483399;

  if ( cg1 < 1 || m1 <= cg1 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'SET_key - Fatal error!\n' );
    fprintf ( 1, '  Input parameter CG1 out of bounds.\n' );
    error ( 'SET_key - Fatal error!' );
  end

  if ( cg2 < 1 || m2 <= cg2 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'SET_key - Fatal error!\n' );
    fprintf ( 1, '  Input parameter CG2 out of bounds.\n' );
    error ( 'SET_key - Fatal error!' );
  end
%
%  Check whether the package must be initialized.
%
  if ( ~ initialized_get ( ) )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'SET_key - Note:\n' );
    fprintf ( 1, '  Initializing RNGLIB package.\n' );
    initialize ( );
  end
%
%  Retrieve the current generator index.
%
  g = cgn_get ( );
%
%  Set the keys.
%
  cg_set ( g, cg1, cg2 );
%
%  Initialize the generator.
%
  t = 0;
  init_generator ( t );

  return
end
