function key = i4_uniform_0i ( key )

%*****************************************************************************80
%
%% i4_uniform_0i() returns a pseudorandom I4.
%
%  Formula:
%
%    key = key * (7^5) mod (2^31 - 1)
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    29 June 2006
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer key, the integer "key" used to generate
%    the output value.  key should not be 0.
%
%  Output:
%
%    integer key, a uniform random value between
%    1 and 2^31-1.
%
%  Local:
%
%    IA = 7^5
%    IB = 2^15
%    IB16 = 2^16
%    IP = 2^31-1
%
  ia = 16807;
  ib15 = 32768;
  ib16 = 65536;
  ip = 2147483647;
%
%  Don't let key be 0.
%
  if ( key == 0 )
    key = ip;
  end
%
%  Get the 15 high order bits of key.
%
  ixhi = floor ( key / ib16 );
%
%  Get the 16 low bits of key and form the low product.
%
  loxa = ( key - ixhi * ib16 ) * ia;
%
%  Get the 15 high order bits of the low product.
%
  leftlo = floor ( loxa / ib16 );
%
%  Form the 31 highest bits of the full product.
%
  iprhi = ixhi * ia + leftlo;
%
%  Get overflow past the 31st bit of full product.
%
  k = floor ( iprhi / ib15 );
%
%  Assemble all the parts and presubtract IP.  The parentheses are
%  essential.
%
  key = ( ( ( loxa - leftlo * ib16 ) - ip ) ...
          + ( iprhi - k * ib15 ) * ib16 ) + k;
%
%  Add IP back in if necessary.
%
  if ( key < 0 )
    key = key + ip;
  end

  return
end
