Device discovery on simple bus
To do this sort of dynamic addressing on a common bus you ultimately need to have one of two things:


a very small address space so you can brute-force search it efficiently
some kind of collision detection/management system


If you don't have either of these two things you will need to hard code addresses into the master and slave devices.

The reason for this is that if you have an address space (of size N) that is too large to search efficiently by stepping through every N address, then any optimised search algorithm that could find the address in less than N steps must include the possibility of more than one slave transmitting a response at the same time. If the physical layer cannot handle this situation, then unfortunately you are out of luck. If the physical layer can detect collisions, then there are many options, such as having a random retry delay when a collision occurs (Ethernet), or using something like CAN bus where collisions are managed in a cooperative way.