adding my translation of the random routing algorithm into SQL