static byte[] generateSecret(BigInteger max) {
    int maxSizeInBytes = bigIntSizeInBytes(max);
    Random rand = new SecureRandom();
    BigInteger r;
    do {
      r = new BigInteger(max.bitLength(), rand);
    } while (r.signum() <= 0 || r.compareTo(max) >= 0);

    return bigIntToByteArray(r, maxSizeInBytes);
  }
Generates a random BigInteger in (0, max) (excluding 0 and max) and converts the result to a
byte array having the same length as max.
java_src/src/main/java/com/google/crypto/tink/hybrid/subtle/RsaKem.java