function r = vdc ( i )

%*****************************************************************************80
%
%% vdc() computes an element of the van der Corput sequence.
%
%  Discussion:
%
%    The van der Corput sequence is often used to generate a "subrandom"
%    sequence of points which have a better covering property
%    than pseudorandom points.
%
%    The van der Corput sequence generates a sequence of points in [0,1]
%    which never repeats.  The elements of the van der Corput sequence 
%    are strictly less than 1.
%
%    The van der Corput sequence writes an integer in a given base 2,
%    and then its digits are "reflected" about the decimal point.
%    This maps the numbers from 1 to N into a set of numbers in [0,1],
%    which are especially nicely distributed if N is one less
%    than a power of the base.
%
%    The generation is quite simple.  Given an integer I, the expansion
%    of I in base 2 is generated.  Then, essentially, the result R
%    is generated by writing a decimal point followed by the digits of
%    the expansion of I, in reverse order.  This decimal value is actually
%    still in base 2, so it must be properly interpreted to generate
%    a usable value.
%
%  Example:
%
%    I        I         van der Corput
%    decimal  binary    binary   decimal
%    -------  ------    ------   -------
%        0  =     0  =>  .0     = 0.0
%        1  =     1  =>  .1     = 0.5
%        2  =    10  =>  .01    = 0.25
%        3  =    11  =>  .11    = 0.75
%        4  =   100  =>  .001   = 0.125
%        5  =   101  =>  .101   = 0.625
%        6  =   110  =>  .011   = 0.375
%        7  =   111  =>  .111   = 0.875
%        8  =  1000  =>  .0001  = 0.0625
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    09 August 2016
%
%  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.
%
%    John Hammersley,
%    Monte Carlo methods for solving multivariable problems,
%    Proceedings of the New York Academy of Science,
%    Volume 86, pages 844-874, 1960.
%
%    Johannes van der Corput,
%    Verteilungsfunktionen I and II,
%    Nederl. Akad. Wetensch. Proc.,
%    Volume 38, 1935, pages 813-820, pages 1058-1066.
%
%  Input:
%
%    Input, integer I, the index of the element of the sequence.
%    I = 0 is allowed, and returns R = 0.
%
%  Output:
%
%    Output, real R, the I-th element of the van der Corput sequence.
%

%
%  Isolate the sign, and only work with the integer part of I.
%
  s = sign ( i );
  t = abs ( floor ( i ) );
%
%  Carry out the computation.
%
  base_inv = 0.5;

  r = 0.0;

  while ( t ~= 0 )
    digit = mod ( t, 2 );
    r = r + digit * base_inv;
    base_inv = base_inv / 2.0;
    t = floor ( t / 2 );
  end
%
%  Recover the sign.
%
  r = r * s;

  return
end

