# Partitioning List Into Sublists of Length 2 With The Pairing Being Random

I've done a bit of Googling with hopes of finding an answer, but to no avail. I'm looking for a way that I can have Mathematica take the elements of a list, say {a,b,c,d}, and partition the elements into lists of length 2 but allowing the grouping of these elements to be random. The Partition function nearly produces the desired results, but groups the elements in {a,b,c,d} in the order they appear (i.e. the result is {{a,b},{c,d}}) when I would like a random pairing such as {{a,d},{c,b}}.

Is there some way of utilizing the Partition function so that it allows me achieve this? Or there perhaps another built-in function or combination of built-in functions that do this?

Thank you all so very much.

• Partition[RandomSample[(* stuff *)], (* stuff *)] ought to work… Jun 5, 2015 at 16:28
• I'm having difficulty applying this to a more sophisticated set that I'm using. It worked well when I applied this to {a,b,c,d}, but when using this with the list I'm working with that is of length 19, it gives me the error "RandomSample cannot generate a random sample of length 2 which is greater than the length of the sample set". UPDATE: I got it to work! Thank you! Jun 5, 2015 at 16:43
• "I got it to work!" - may I recommend writing an answer to your question to show how you made it work? ;) Jun 5, 2015 at 16:49
• It was a simple syntax error on my part. I called the list I was working with ConvertedList and initially used the following input Partition[RandomSample[{ConvertedList}],5],2] which resulted in an error because the curly brackets weren't necessary. Writing Partition[RandomSample[ConvertedList],5],2] fixed the issue. Jun 5, 2015 at 19:58
• May I ask if there is a way to get all the possible ways of pairing? (instead of just a random pairng) Oct 19, 2020 at 12:25

While David G. Stork's solution works it is not efficient. You are generating all possible tuples and then selecting them randomly. Try this with a list of 19 elements in tuples of 10 and you will be waiting forever.

This solution should work just fine with lists of any length

list = {a, b, c, d};
Partition[
RandomSample[list]
, 2]

{{c, d}, {b, a}}


@Qwerty to produce a specific number of pairs, you should try something like this:

list = {a, b, c, d, e, f, g};
numberOfPairs = 3;
Partition[
RandomSample[list, numberOfPairs*2]
, 2]

{{f, a}, {g, c}, {e, d}}


Note that Length@list must be greater or equal to 2*numberOfPairs

• If you do just RandomSample[list], you will only get one out of the many permutations of list. Jun 5, 2015 at 17:08
• But you said that you just want one random pairing. This approach gives give you a random pairing each time. Jun 5, 2015 at 17:15
• So does my suggestion; try it out. Jun 5, 2015 at 17:27
• oh.... you mean to get rid of "Length@list" right? It does work. Im changing the answer based on that. Jun 5, 2015 at 17:37
• Exactly. $\phantom{}$ Jun 5, 2015 at 17:38
RandomSample[Tuples[{a, b, c, d}, 2]]


If you want a fixed number of pairs (say 5):

Take[RandomSample[Tuples[{a, b, c, d}, 2]], 5]

• This works quiet well! Is there a way of forcing it to produce a specific number of pairs? Jun 5, 2015 at 16:34

Could this work for your problem?

A = {a, b, c, d, e, f, g}
list = Union[Flatten[Map[Partition[#, 2] &, Permutations[A]], 1]]
Extract[list, RandomInteger[{1, Length[list]}]]


Change the predicate in the Partition function for different lengths.

f[_] := Extract[list, RandomInteger[{1, Length[list]}]]
Array[f, 10]


will generate 10 subsets for (pseudo)random pairs.