def randf_2(idum,iy,iv):
   import numpy as np
#
# 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
   jmax=40

# initial iseed (idum) is negative

   if  idum <= 0 or iy == 0:
      idum=max(-idum,1)
      for jj in range(0,jmax):
         j=ntab+8-jj
         k=np.floor(idum/iq)
         idum=ia*(idum-k*iq)-ir*k
         if idum < 0:
            idum=idum+im
         if j <= ntab:
            iv[j]=idum
      iy=iv[1]

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

   if  idum <= 0:
     idum=idum+im
   j=1+int(np.floor(iy/ndiv))
   iy=iv[j]
   iv[j]=idum
   iseed=idum
   ran1=min(am*iy,rnmx)
    

   return ran1,iseed,iy,iv
