I have an array, and I'd like to shuffle the array so that no element moves more than K places from its initial position in the array. So for example, if K is 2, and the original array is:
[1,2,3,4,5,6,7,8]
this is a valid permutation:
[1,3,2,5,4,8,6,7]
but this is not:
[1,2,5,4,6,8,3,7]
because the 3
has moved 4 places.
Is there a way to do this so that it's a perfectly random shuffle (in other words, all permutations that satisfy this criteria are equally likely?)
Aucun commentaire:
Enregistrer un commentaire