1
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ Partition[RandomSample[(* stuff *)], (* stuff *)] ought to work… $\endgroup$ Commented Jun 5, 2015 at 16:28
  • $\begingroup$ 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! $\endgroup$
    – Qwerty
    Commented Jun 5, 2015 at 16:43
  • $\begingroup$ "I got it to work!" - may I recommend writing an answer to your question to show how you made it work? ;) $\endgroup$ Commented Jun 5, 2015 at 16:49
  • $\begingroup$ 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. $\endgroup$
    – Qwerty
    Commented Jun 5, 2015 at 19:58
  • $\begingroup$ May I ask if there is a way to get all the possible ways of pairing? (instead of just a random pairng) $\endgroup$
    – Ogawa Chen
    Commented Oct 19, 2020 at 12:25

3 Answers 3

2
$\begingroup$

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

$\endgroup$
6
  • $\begingroup$ If you do just RandomSample[list], you will only get one out of the many permutations of list. $\endgroup$ Commented Jun 5, 2015 at 17:08
  • $\begingroup$ But you said that you just want one random pairing. This approach gives give you a random pairing each time. $\endgroup$
    – elbOlita
    Commented Jun 5, 2015 at 17:15
  • $\begingroup$ So does my suggestion; try it out. $\endgroup$ Commented Jun 5, 2015 at 17:27
  • $\begingroup$ oh.... you mean to get rid of "Length@list" right? It does work. Im changing the answer based on that. $\endgroup$
    – elbOlita
    Commented Jun 5, 2015 at 17:37
  • $\begingroup$ Exactly. $\phantom{}$ $\endgroup$ Commented Jun 5, 2015 at 17:38
0
$\begingroup$
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]
$\endgroup$
1
  • $\begingroup$ This works quiet well! Is there a way of forcing it to produce a specific number of pairs? $\endgroup$
    – Qwerty
    Commented Jun 5, 2015 at 16:34
0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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