function [ran1,iseed,iy,iv]=randf_2(idum,iy,iv)
%
% generates a random number 0 < x < 1
% “Minimal” random number generator of Park and Miller with Bays-Durham shuffle and
% added safeguards. Returns a uniform random deviate between 0.0 and 1.0 (exclusive of
% the endpoint values). Call with idum a negative integer to initialize; thereafter, do not
% alter idum between successive deviates in a sequence. RNMX should approximate the largest
% floating value that is less than 1.
%
% Park, S.K., and Miller, K.W. 1988, Communications of the ACM, vol. 31, pp. 1192–1201. 



ia=16807;
im=2147483647;
am=1/im;
iq=127773;
ir=2836 ;
ntab=32;
ndiv=1+(im-1)/ntab;
eps=1.2e-7;
rnmx=1-eps;

% initial iseed (idum) is negative

if  idum <= 0 || iy == 0
   idum=max(-idum,1);
   for j=ntab+8:-1:1
      k=floor(idum/iq);
      idum=ia*(idum-k*iq)-ir*k;
      if idum < 0
         idum=idum+im;
      end
      if j <= ntab
         iv(j)=idum;
      end 
   end 
   iy=iv(1);
end 

k=floor(idum/iq);
idum=ia*(idum-k*iq)-ir*k;

if  idum <= 0
  idum=idum+im;
end
j=1+floor(iy/ndiv);
iy=iv(j);
iv(j)=idum;
iseed=idum;
ran1=min(am*iy,rnmx);


