1 - Turn off transponder.
2 - Select new code.
3 - Turn on transponder.
Anything less than the above results in the confusion already described.
If you are cycling through codes which involve (from or to), the number 7 (i.e. 4500 to 7000) there is a very high likelyhood that for just a second your transponder will transmit 7500 - hijack. This will instantly be displayed to on every radar which has SSR within at leat 60 miles, plus to the distress and diversion cell, and many military statitions - ground based and airborne.
Under current circumstances they all would be within their rights to think that you were trying to indicate that you were being threatened, and had a real problem.
It sounds bizzare, but I suggest you don't try it out for real.