function r = i4_to_van_der_corput_sequence ( key, base, n )

%*****************************************************************************80
%
%% i4_to_van_der_corput_sequence(): next N elements of a van der Corput sequence.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    27 February 2003
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    John Halton,
%    On the efficiency of certain quasi-random sequences of points
%    in evaluating multi-dimensional integrals,
%    Numerische Mathematik,
%    Volume 2, pages 84-90, 1960.
%
%    Johannes van der Corput,
%    Verteilungsfunktionen I and II,
%    Nederl. Akad. Wetensch. Proc.,
%    Volume 38, 1935, pages 813-820, pages 1058-1066.
%
%  Input:
%
%    integer key, the key or index of the desired element.
%    key should be nonnegative.
%    key = 0 is allowed, and returns R = 0.
%
%    integer BASE, the van der Corput base, which is typically
%    a prime number.  BASE must be greater than 1.
%
%    integer N, the number of elements to compute.
%
%  Output:
%
%    real R[N], the key through key + N - 1 elements of
%    the van der Corput sequence for base BASE.
%
  r(1:n) = 0.0;
%
%  Ensure that BASE is an acceptable integer.
%
  base = floor ( base );

  if ( base <= 1 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'I4_TO_VAN_DER_CORPUT_SEQUENCE - Fatal error!\n' );
    fprintf ( 1, '  The input base BASE is <= 1!\n' );
    fprintf ( 1, '  BASE = %d\n', base );
    error ( 'I4_TO_VAN_DER_CORPUT_SEQUENCE - Fatal error!' );
  end
%
%  Ensure that key is an acceptable integer.
%
  key = floor ( key );

  if ( key < 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'I4_TO_VAN_DER_CORPUT_SEQUENCE - Fatal error!\n' );
    fprintf ( 1, '  The input base key is < 0!\n' );
    fprintf ( 1, '  key = %d\n', key );
    error ( 'I4_TO_VAN_DER_CORPUT_SEQUENCE - Fatal error!' );
  end
%
%  Carry out the computation.
%
  key2(1:n) = key:1:key+n-1;
  base_inv = 1.0 / base;

  while ( any ( key2 ~= 0 ) )
    digit = mod ( key2, base );
    r = r + digit * base_inv;
    base_inv = base_inv / base;
    key2 = floor ( key2 / base );
  end

  return
end
