I need a algorithm that sorts a list in a consistent way when given a input seed number and a list of unique items to shuffle. Here are the properties it must satisfy: Let's call the algorithm function(list, seed_value)
- No randomness which means function([1, 2, 3, 4], n) is the same as function([1, 2, 3, 4], n) the next time it is called
- Every seed number that is more than 0 and less than or equal to the number of permutations of the list possible must give a unique shuffled list from the others.
- It is an algorithm, not a lookup table
- Efficient, timewise and spacewise
- As few loops as possible
- Easy to understand if possible
It's... a bit hard for me to comprehend so I am asking you people to help. (btw even if it is not really random it doesn't matter for me)
Here is an implementation of n-th permutation.
Here is why it works. When we arrive at the
mth iteration of the loop, the values at indexes0, 1, ..., m-1have already been shuffled. We then swap them'th index with any one of them based on the remainder ofn / (m+1). And now the values at indexes0, 1, ..., mhave been shuffled. We dividenbym+1to get rid of the information we used, and we arrive at the next loop.