function rng_test ( )

%*****************************************************************************80
%
%% rng_test() tests rng().
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    05 September 2012
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'RNG_TEST():\n' );
  fprintf ( 1, '  Test the MATLAB random number functions RNG and RAND.\n' );
%
%  No initialization.
%
  for i = 1 : 5
    rng_test01 ( );
  end
%
%  Same initialization.
%
  key = 123456789;
  for i = 1 : 5
    rng_test02 ( key );
    key = key;
  end
%
%  Varying initialization.
%
  key = 123456789;
  for i = 1 : 5
    rng_test02 ( key );
    key = 123456789 + mod ( i, 3 );
  end
%
%  Random initialization.
%
  for i = 1 : 5
    rng_test03 ( );
  end
%
%  Save and restore state.
%
  rng_test04 ( );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'RNG_TEST:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end
function rng_test01 ( )

%*****************************************************************************80
%
%% rng_test01() calls RAND with no initialization.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    05 September 2012
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'RAND_TEST01:\n' );
  fprintf ( 1, '  Call RAND with no initialization:\n' );
  fprintf ( 1, '\n' );

  for i = 1 : 5
    r = rand ( );
    fprintf ( 1, '  %d  %g\n', i, r );
  end

  return
end
function rng_test02 ( key )

%*****************************************************************************80
%
%% rng_test02() initializes random number generator with key.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    05 September 2012
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'RAND_TEST02:\n' );
  fprintf ( 1, '  Initialize random number generation with RNG(%d):\n', key );
  fprintf ( 1, '\n' );

  rng ( key );

  for i = 1 : 5
    r = rand ( );
    fprintf ( 1, '  %d  %g\n', i, r );
  end

  return
end
function rng_test03 ( )

%*****************************************************************************80
%
%% rng_test03() randomly initializes the random number generator.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    05 September 2012
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'RAND_TEST03:\n' );
  fprintf ( 1, '  Randomly initialize with RNG(''shuffle''):\n' );
  fprintf ( 1, '\n' );

  rng ( 'shuffle' );

  for i = 1 : 5
    r = rand ( );
    fprintf ( 1, '  %d  %g\n', i, r );
  end

  return
end
function rng_test04 ( )

%*****************************************************************************80
%
%% rng_test04() saves the state and restores it later.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    05 September 2012
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'RAND_TEST04:\n' );
  fprintf ( 1, '  Save and restore the RNG state:\n' );
  fprintf ( 1, '\n' );

  s = rng ( );

  for i = 1 : 5
    r = rand ( );
    fprintf ( 1, '  %d  %g\n', i, r );
  end

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Restoring RNG state:\n' );
  fprintf ( 1, '\n' );

  rng ( s );

  for i = 1 : 5
    r = rand ( );
    fprintf ( 1, '  %d  %g\n', i, r );
  end
  return
end

