Shuffle the list with the maximum distance traveled

Say I have a list [a, b, c, d, e, f, g]that I want to shuffle.

I would like to find an algorithm that randomly rearranges this data with the restriction that no element will be more than N spaces from its original position. Thus, c N = 2, awill never appear below the third position of the start list. Using the N = 0algorithm will return the list without any changes and N = len(list) - 1, it will function exactly like a regular shuffling algorithm

For example, using python syntax:

>>> restrictedShuffle([a, b, c, d, e, f, g], N=2)
[a, c, b, f, d, g, e] 
>>> restrictedShuffle([a, b, c, d, e, f, g], N=1)
[b, a, c, e, d, f, g]
>>> restrictedShuffle([a, b, c, d, e, f, g], N=0)
[a, b, c, d, e, f, g]

Is there such an algorithm?

+4
source share

:

17

:

3474
?
3235
, ?
2849
?
2227
?
1782
?
710

Source: https://habr.com/ru/post/1683368/


All Articles