how do i do a random swap but impose a limit of how far that number can move from its original position?

Viewed 129

A set of 20 numbers have been stored inside a vector d, for example:

d = [ 5 6 7 8 9 ....]

I use

i = randperm(length(d));
d = d(i);

to randomly shuffle the numbers inside the matrix.

However, I need to find a way to limit the shuffle to ensure that the number does not move more then "5" places from its original position?

Meaning that if originally d(2) = 6, the final position of 6 should only move to d(1) to d(2+5).

Note, d(1) because the numbers cannot move to a negative position.

Any help on this would be appreciated! also, if there is a more efficient way with the shuffling please kindly let me know!

2 Answers
Related