How about using

next_port_to_try = 49152 + rand(65535 - 49152)

If we retry 10 times then the chance that we will try the same port twice is less than 0.05%; given that we will gracefully handle a repeated retry, I'd say there is no harm in doing it this way.