function rand_test ( )

%*****************************************************************************80
%
%% RAND_TEST shows how random numbers are generated in MATLAB.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license. 
%
%  Modified:
%
%    11 June 2012
%
%  Author:
%
%    John Burkardt
%
  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'RAND_TEST:\n' );
  fprintf ( 1, '  MATLAB version\n' );
  fprintf ( 1, '  Test the MATLAB RAND function.\n' );

  rand_test01 ( );

  seed = 123456789;
  rand_test02 ( seed );

  seed = 987654321;
  rand_test02 ( seed );

  seed = 123456789;
  rand_test02 ( seed );

  rand_test03 ( );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'RAND_TEST:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  fprintf ( 1, '\n' );
  timestamp ( );

  return
end
function rand_test01 ( )

%*****************************************************************************80
%
%% RAND_TEST01 simply calls the random number generator a few times.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license. 
%
%  Modified:
%
%    22 October 2009
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'RAND_TEST01:\n' );
  fprintf ( 1, '  In MATLAB, unit random numbers are generated by calling RAND:\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  A = rand ( )      a random scalar value.\n' );
  fprintf ( 1, '  B = rand ( 5, 1 ) a random column vector of 5 entries.\n' );
  fprintf ( 1, '  C = rand ( 1, 5 ) a random row vector of 5 entries.\n' );
  fprintf ( 1, '  D = rand ( 3, 4 ) a 3 by 4 random matrix.\n' );
  fprintf ( 1, '  E = rand ( 5 )    a 5 by 5 random matrix.\n' );

  a = rand ( )
  b = rand ( 5, 1 )
  c = rand ( 1, 5 )
  d = rand ( 3, 4 )
  e = rand ( 5 )

  return
end
function rand_test02 ( seed )

%*****************************************************************************80
%
%% RAND_TEST02 sets the seed before calling RAND.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license. 
%
%  Modified:
%
%    11 June 2012
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'RAND_TEST02:\n' );
  fprintf ( 1, '  By setting the random number seed, you can control\n' );
  fprintf ( 1, '  how the random number sequence begins.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  The command "rng ( 123456789 )" sets the seed to 123456789.\n' );

  seed = 123456789;

  rng ( seed );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Seed has been set to %d\n', seed );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Now generate 5 random values.\n' );

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

  rng ( seed );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Seed has been reset to %d\n', seed );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Now generate 5 more random values.\n' );

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

  return
end
function rand_test03 ( )

%*****************************************************************************80
%
%% RAND_TEST03 generate a lot of random values.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license. 
%
%  Modified:
%
%    22 October 2009
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'RAND_TEST03:\n' );
  fprintf ( 1, '  Compute a lot of random values.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  The average should be 0.5\n' );
  fprintf ( 1, '  The variance should be 1/12.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '         N    Ave        Ave-1/2   Var        Var-1/12\n' );
  fprintf ( 1, '\n' );

  n = 1;

  for n_log = 0 : 7
    x = rand ( n, 1 );
    x_ave = sum ( x(1:n,1) ) / n;
    x_var = var ( x(1:n,1) );
    fprintf ( 1, '  %8d  %f  %9.2e  %f  %9.2e\n', ...
      n, x_ave, x_ave - 0.5, x_var, x_var - 1.0 / 12.0 );
    n = n * 10;
  end

  return
end
function timestamp ( )

%*****************************************************************************80
%
%% TIMESTAMP prints the current YMDHMS date as a timestamp.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    14 February 2003
%
%  Author:
%
%    John Burkardt
%
  t = now;
  c = datevec ( t );
  s = datestr ( c, 0 );
  fprintf ( 1, '%s\n', s );

  return
end
