I'm trying to shuffle an array but I want to keep some values in tact. This excellent answer here on SO shows how to shuffle but I want to be able to specify an additional parameter that takes an array of values to keep.
This is what I'm trying to achieve. Say I have an array:
[1, 2, 3, 4, 5]
I want to run my shuffle function like this:
shuffle(arr, [1, 3])
This means, shuffle the array but keep arr[1]
and arr[3]
in tact. So I should get:
[3, 2, 1, 4, 5]
[5, 2, 3, 4, 1] etc...
Notice how 2
and 4
never changed their places.
Any help is appreciated!