Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Imagine I have some list:

listA = {"a","b","c","d","e","f","g","h","i","j"};

I'd like to randomly select k positions in the list, for example k = 3 and we select positions {1,9,10}, then randomly shuffle the elements at these positions e.g. giving us ONE of the following five possible outcomes:

listApartialscramble1 = {"a","b","c","d","e","f","g","h","i","j"};
listApartialscramble1 = {"a","b","c","d","e","f","g","h","j","i"};
listApartialscramble2 = {"i","b","c","d","e","f","g","h","j","a"};
listApartialscramble3 = {"i","b","c","d","e","f","g","h","a","j"};
listApartialscramble4 = {"j","b","c","d","e","f","g","h","i","a"};
listApartialscramble5 = {"j","b","c","d","e","f","g","h","a","i"};

Is there a simple way to do this, perhaps with RandomSample?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

k = 5;
set = RandomSample[Range@Length@listA, k]

listA[[ set]] = listA[[ RandomSample@set]];
{8, 3, 10, 1, 4}
{"g", "b", "c", "d", "j", "f", "e", "h", "i", "a"}
share|improve this answer
Funny, I've always written this operation listA[[set]] = RandomSample @ listA[[set]] -- it never occurred to me to shuffle the indices though the effect is equivalent. – Mr.Wizard Apr 15 '14 at 20:06
@Mr.Wizard :) I'm probably biased by the question wording where positions/indices are stressed. That being said, I don't know now if I'd use your approach but yes, I haven't thought about it :) – Kuba Apr 15 '14 at 20:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.