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

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a multiset {1, 3, 3, 1, 2} and would like to randomly permute the classes like so: {2, 1, 1, 2, 3} or {3, 2, 2, 3, 1}, what is the simplest and most efficient way to do this?

In reality the input set would be like RandomInteger[1000, 1000000]


The tricky part of this question is that the positions of the same integers must remain the same! Only the representatives are permuted, but the positions of the classes of integers need to remain the same.

share|improve this question
RandomSample does not preserve the structure. – M.R. Aug 13 '12 at 17:22
For the not-so-bright among us, could you explain how the two sets are permutations of each other and how the positions of the integers remain the same? I don't see theconnection. I see you've been referring answerers to your example, but the example is unhelpful – R. M. Aug 13 '12 at 20:31
@r.m. As I understand it the integers should be seen as labels for various items on fixed locations. Duplicate items are possible (and they have corresponding labels). What Mike wants is to swap the labels but not the items. – Sjoerd C. de Vries Aug 13 '12 at 22:52
up vote 6 down vote accepted

You could do something like this:

list = RandomInteger[1000,1000000];
rules = Dispatch@Thread[Range[0, 1000] -> RandomSample[Range[0, 1000]]];
newlist = list /. rules; 

Thread creates rules for the integers from 1-1000 to a random permutation of the same integers. /.then does the replacement.

Edit: I added Dispatch as per Oleksandr R.'s comment. It cuts the time on my machine from 8.3 seconds to 0.2 seconds.

share|improve this answer
Another way, much faster still: (RandomSample@Range[0, 1000])[[list + 1]]. – Oleksandr R. Aug 13 '12 at 18:17
@Oleksandr That method deserves an answer of its own. Why don't you post it? – Mr.Wizard Jan 13 '14 at 16:21

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.