I have
s = {2, 3, 5, 7, 1, 4, 6} (* always with diffente elements in {1,2....7}*)
l = Length[s]
n = 3
and n
tells us that we want to select 3 positions in the list s
, and permute them within the full list.
For example,
a = RandomSample[Range[l], n]
Suppose we obtain a = {2, 6, 4}
and we apply a random permutation to these positions obtaining {4, 6, 2}
. Then
Position 2 goes to position 4
Position 6 goes to position 6
Position 4 goes to position 2
obtaining
s = {2, 7, 5, 3, 1, 4, 6}
What´s a simple (and comprehensible) way without using an auxiliary list.
Edit
After reading MrWizard an Yi-Wang answers: (I like the s[[a]] =s[[b]]
of MWizard, and the use of permutations and rule of Yi-Wang, but I think that a simpler an more comprehensible way is
s = {2, 3, 5, 7, 1, 4, 6}
n = 3
Print["BEFORE -> ", s]
choice = RandomSample[Range[Length[s]], n]
choicepermuted = RandomSample[choice, n]
s[[choice]] = s[[choicepermuted]]
Print["AFTER -> ", s]