One thing you can consider is the random selection of items. You can select a random position in your collection, and then change the element in this position to the next element. Thus, you can prevent the exchange from 1 to 3 or 2 to 4. You can do this again until the numbers are correctly scrambled:
[1, 2, 3, 4] random number is 0, swap with the element at position 1.
[2, 1, 3, 4] random number is 1, swap with the element in position 2.
elements 1 and 3, so do not change them.
[2, 1, 3, 4] random number is 2, swap with the element in position 3.
[2, 1, 4, 3] , etc.
If you want to generalize the constraint, you can simply change this condition. Instead of refusing to exchange when elements 1 or 3, or 2 and 4 (as in the example above), you can make sure that the two elements in the positions to be exchanged are not within 2 of each other, so something like if(b==a+2)continue; :
elements are 5 and 7, so do not change them.
if(7==5+2)continue;
source share