function g = cgn_memory ( i, g )

%*****************************************************************************80
%
%% cgn_memory() stores the current generator index.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    30 March 2013
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer I, the desired action.
%    -1, get the value.
%    0, initialize the value.
%    1, set the value.
%
%    integer G, for I = 1, the new generator index..
%
%  Output:
%
%    integer G.  For I = -1 or 0, the current or initialized 
%    generator index.
%
  g_max = 32;

  persistent g_save;

  if ( i < 0 )

    g = g_save;

  elseif ( i == 0 )

    g_save = 1;
    g = g_save;

  elseif ( 0 < i )

    if ( g < 1 | g_max < g )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'CGN_MEMORY - Fatal error!\n' );
      fprintf ( 1, '  Input generator index G is out of bounds.\n' );
      error ( 'CGN_MEMORY - Fatal error!' );
    end

    g_save = g;

  end

  return
end
