4

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

I'd like to find algorithm that would randomly permute that data with the restriction that no element would be more than N spaces away from its starting position. So with N = 2, a would never appear below the third position of the starting list. With N = 0, the algorithm would return the list without any changes, and N = len(list) - 1, it would function exactly like a normal 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]

Does such an algorithm exist?

Dadep
  • 2,796
  • 5
  • 27
  • 40

0 Answers0