code: def deterministic_generate_k(generator_order, secret_exponent, val, hash_f=hashlib.sha256):
      
      n = generator_order
      bln = bit_length(n)
      order_size = (bln + 7) // 8
      hash_size = hash_f().digest_size
      v = b'\x01' * hash_size
      k = b'\x00' * hash_size
      priv = intstream.to_bytes(secret_exponent, length=order_size)
      shift = 8 * hash_size - bln
      if shift > 0:
          val >>= shift
      if val > n:
          val -= n
      h1 = intstream.to_bytes(val, length=order_size)
      k = hmac.new(k, v + b'\x00' + priv + h1, hash_f).digest()
      v = hmac.new(k, v, hash_f).digest()
      k = hmac.new(k, v + b'\x01' + priv + h1, hash_f).digest()
      v = hmac.new(k, v, hash_f).digest()
  
      while 1:
          t = bytearray()
  
          while len(t) < order_size:
              v = hmac.new(k, v, hash_f).digest()
              t.extend(v)
  
          k1 = intstream.from_bytes(bytes(t))
  
          k1 >>= (len(t)*8 - bln)
          if k1 >= 1 and k1 < n:
              return k1
  
          k = hmac.new(k, v + b'\x00', hash_f).digest()
          v = hmac.new(k, v, hash_f).digest()
docs: :param generator_order: result from :method:`pycoin.ecdsa.Generator.Generator.order`,
          necessary to ensure the k value is within bound
      :param secret_exponent: an integer secret_exponent to generate the k value for
      :param val: the value to be signed, also used as an entropy source for the k value
      :returns: an integer k such that ``1 <= k < generator_order``, complying with
          <https://tools.ietf.org/html/rfc6979>