1
$\begingroup$

I'm trying to produce $n$ random samples from a list, but not all samples, and I'd like the samples to not repeat in the same execution of the command. If I'm not mistaken, something like Permutations[] for not all permutations, or RandomPermutation for a list, or a hypothetical MultipleRandomSample[]. Is there something I'm misinterpreting or a way to generate such collections of random samples?

Edit: What I'm calling random samples are actually subsets, since they are fixed size and order doesn't matter. Now, I only need to take $n$ random subsets from a set.

RandomSample repeats "subsets":

Table[RandomSample[{2, 3, 5, 8}, 3], 10]
{{3, 2, 5}, {5, 8, 2}, {8, 3, 2}, {8, 5, 2}, {2, 3, 5}, {8, 2, 3}, {2,
   3, 8}, {2, 5, 3}, {8, 3, 5}, {2, 8, 5}}

Subsets is adequate, but it gives all subsets, and I wish to find $n$ random subsets:

Subsets[{2, 3, 5, 8}, {3}]
{{2, 3, 5}, {2, 3, 8}, {2, 5, 8}, {3, 5, 8}}

A simulated result would be:

Table[Op[{2, 3, 5, 8}, {3}],3]
{{{2, 3, 5}, {2, 3, 8}, {2, 5, 8}},
{{2, 3, 5}, {2, 3, 8}, {3, 5, 8}},
{{2, 3, 8}, {2, 5, 8}, {2, 5, 8}},
{{2, 3, 8}, {2, 5, 8}, {2, 5, 8}}(*repetitions are ok*)}

Is there a way to produce such collections of subsets without obtaining all subsets, randomizing, and taking $n$?

$\endgroup$
  • 3
    $\begingroup$ Why doesn't Table[RandomSample[list, k], {n}] suit your needs? $\endgroup$ – J. M. will be back soon Mar 29 at 14:22
  • $\begingroup$ I don't know if RandomSample[] could repeat. $\endgroup$ – pedroos Mar 29 at 14:57
  • $\begingroup$ Under the "Details" section of the docs: "RandomSample ... never samples ... more than once." $\endgroup$ – J. M. will be back soon Mar 29 at 15:01
  • $\begingroup$ I take that to mean it wouldn't repeat elements in a sample, not that it wouldn't select the same sample again, no? $\endgroup$ – pedroos Mar 29 at 15:06
  • 3
    $\begingroup$ Then perhaps something like Partition[RandomSample[list], k] to generate all non-overlapping k-samples, and take the first few as needed? $\endgroup$ – J. M. will be back soon Mar 29 at 15:14
2
$\begingroup$

You just need to use the 3-arg syntax for Subsets. Here is a function that does this:

MultipleRandomSubsets[list_, length_, count_] := Module[{total},
    total = Binomial[Length @ list, length];
    Join @@ Map[Subsets[list, {length}, {#}]&] @ RandomSample[1 ;; total, count] /; count <= total
]

Small example:

SeedRandom[1]
MultipleRandomSubsets[{2,3,5,8}, 3, 3]

{{3, 5, 8}, {2, 3, 8}, {2, 3, 5}}

An example that couldn't be done by enumerating all samples:

SeedRandom[1]
MultipleRandomSubsets[Range[100], 10, 10]

{{16, 23, 32, 33, 45, 49, 70, 72, 85, 92}, {17, 18, 31, 41, 43, 64, 80, 87, 88, 97}, {1, 13, 18, 37, 40, 43, 46, 48, 61, 88}, {5, 19, 22, 40, 43, 53, 58, 68, 73, 88}, {19, 28, 60, 76, 83, 85, 95, 96, 98, 99}, {9, 30, 32, 46, 53, 66, 77, 86, 89, 90}, {1, 9, 19, 20, 21, 28, 51, 76, 80, 96}, {9, 16, 22, 61, 67, 69, 79, 82, 98, 99}, {8, 34, 40, 43, 80, 82, 84, 87, 96, 98}, {13, 34, 38, 44, 47, 48, 49, 67, 74, 80}}

$\endgroup$
  • 1
    $\begingroup$ It's good to explicitly point out two lesser-known features: 1. RandomSample can take a Span object. This is critical, as we couldn't possibly generate the entire (huge) integer range to sample from. 2. Subsets can directly generate the nth subset. These two features make it possible to defeat combinatorial explosion. $\endgroup$ – Szabolcs Apr 12 at 9:40
1
$\begingroup$

This is not an answer, but a request for clarification that can't be made in a comment.

I'm confused by your question because it seems to me to be open to several interpretations. Here are three that immediately occur to me.

Suppose that sample is a function, taking no argument, that will return a random sample meeting your requirements, and suppose that we use it to generate a set of samples. Note: it may be the sample can not be written — we might need to generate the whole sample set at once and not sample-by-sample — but we will assume it exists for the sake of discourse.

Sample generation

sampleSet = With[{n = 5}, Do[sample[], n]];

Now, which of the following, increasingly stringent requirements does sampleSet have to meet?

require[1] = Flatten[Intersection /@ sampleSet] == {}

require[2] = Intersection @@ sampleSet == {}

require[3] = Module[{smpl = Flatten[sampleSet]}, Sort[smpl] == Union[smpl]]

Or is it none of the above? If the latter, please state your requirements in Mathematica code.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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