function set_initial_key ( ig1, ig2 )

%*****************************************************************************80
%
%% set_initial_key() resets the initial key and state for all generators.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    27 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 IG1, IG2, the initial key values
%    for the first generator.
%    1 <= IG1 < 2147483563
%    1 <= IG2 < 2147483399
%
  a1_vw = 2082007225;
  a2_vw = 784306273;
  g_max = 32;
  m1 = 2147483563;
  m2 = 2147483399;

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

  if ( ig2 < 1 || m2 <= ig2 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'SET_INITIAL_key - Fatal error!\n' );
    fprintf ( 1, '  Input parameter IG2 out of bounds.\n' );
    error ( 'SET_INITIAL_key - Fatal error!' );
  end
%
%  Because INITIALIZE calls SET_INITIAL_key, it's not easy to correct
%  the error that arises if SET_INITIAL_key is called before INITIALIZE.
%  So don't bother trying.
%
  if ( ~ initialized_get ( ) )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'SET_INITIAL_key - Fatal error!\n' );
    fprintf ( 1, '  The RNGLIB package has not been initialized.\n' );
    error ( 'SET_INITIAL_key - Fatal error!' );
  end
%
%  Set the initial key, then initialize the first generator.
%
  g = 1;
  cgn_set ( g );

  ig_set ( g, ig1, ig2 );

  t = 0;
  init_generator ( t );
%
%  Now do similar operations for the other generators.
%
  for g = 2 : g_max

    cgn_set ( g );
    ig1 = multmod ( a1_vw, ig1, m1 );
    ig2 = multmod ( a2_vw, ig2, m2 );
    ig_set ( g, ig1, ig2 );
    init_generator ( t );

  end
%
%  Now choose the first generator.
%
  g = 1;
  cgn_set ( g );

  return
end
