public static int[] getRandomAvailableTCPPortRange(final int count) {
    int[] ports = new int[count];
    boolean needMorePorts = true;

    while (needMorePorts) {
      int base = nextCandidatePort.getAndUpdate(skipCandidatePorts(count));
      if (base + count > AVAILABLE_PORTS_UPPER_BOUND) {
        continue;
      }

      needMorePorts = false; // Assume we'll find enough in this batch
      for (int i = 0; i < count; i++) {
        int port = base + i;
        if (isPortAvailable(port, SOCKET, getAddress(SOCKET))) {
          ports[i] = port;
          continue;
        }
        needMorePorts = true;
        break;
      }
    }
    return ports;
  }
Returns the requested number of consecutive available tcp ports.
geode-junit/src/main/java/org/apache/geode/internal/AvailablePortHelper.java