jeudi 23 mars 2017

Shuffle a list, but only slightly

In Python, I would like to shuffle a list, in such a way that each element ends up no more than N elements away from where it started, where N is a constant. Also, I would like this to be fair. That is, every permutation that satisfies this constraint should be equally likely (within practical limits of random number generators).

For example, if N is 3, and the input is [1, 2, 3, 4, 5, 6], then the result [2, 1, 3, 6, 4, 5] would be valid, but [6, 4, 1, 3, 5, 2] would not be, because 6 and 2 are too far away from their starting location.

Is there an easy way to do this in Python? If not, is there some existing algorithm which will do this? Pseudocode is fine, I can implement it in Python if necessary.

Runtime is not incredibly important, because I will only run this shuffle on ~100k elements once every few minutes, so I can stand to wait a few seconds for it to run, if necessary.




Aucun commentaire:

Enregistrer un commentaire