public static int getRandomAvailablePortInRange(int rangeBase, int rangeTop, int protocol) {
    int numberOfPorts = rangeTop - rangeBase;
    // do "5 times the numberOfPorts" iterations to select a port number. This will ensure that
    // each of the ports from given port range get a chance at least once
    int numberOfRetrys = numberOfPorts * 5;
    for (int i = 0; i < numberOfRetrys; i++) {
      // add 1 to numberOfPorts so that rangeTop also gets included
      int port = rand.nextInt(numberOfPorts + 1) + rangeBase;
      if (isPortAvailable(port, protocol, getAddress(protocol))) {
        return port;
      }
    }
    return -1;
  }
Returns a randomly selected available port in the range 5001 to 32767.

@param protocol The protocol to check (either {@link #SOCKET} or {@link #MULTICAST}).
@param addr the bind-address or mcast address to use
@throws IllegalArgumentException <code>protocol</code> is unknown
geode-membership/src/main/java/org/apache/geode/internal/membership/utils/AvailablePort.java