How to pick exclusively a port in a range of ports

Hello there!
I have some custom web services running behind nginx.
I don’t care what port they use to expose themselves to nginx, but I need them to use different ports for it to work. I’d like to use some nix to delegate this behavior.
I would like to define a list of open ports (for example, lib.range 9000 9100) and to pick one of them for each web service.
Is it possible to achieve this? I’m not sure if this can be a deterministic process.

I’ve had that issue as well but couldn’t come up with an solution for assigning ports(everything I could come up with ended with infinite recursion). Instead I’ve just used Unix sockets named after the service.

Oh, thanks. I don’t think I can use Unix sockets with my setup, but it’s an interesting workaround