You're considering 9 tiles at most (dx=-1/0/+1,dy=-1/0/+1).
In that case it's easier/faster/deterministic to iterate over all 9 tiles, put those that are acceptable into a vector and finally select a random value from that vector.