Whenever a spell or ability is put onto the stack, if it has a single target, reselect its target at random. (Select from among all legal targets.)
The ability triggers when a spell or ability is put on the stack, and this target-changing ability itself goes on the stack. This means that the spell or ability keeps its original target until this ability resolves. You determine the new target when this ability resolves, at which time the set of legal targets may have changed.