I'm looking for a simple way to generate random samples of lists of integers such that each time I sample I'm sure it chooses a new random sample. This is closely related to Picking random items out of a list only once except that I cannot just generate all lists of samples because it is way too large.


For example I would like to find a (1 is enough) sample of 40 elements from a set of 100 that have a certain property. Generating all random samples is impossible and while we could generate them one by one I ould prefer to take random samples.

  • 2
    $\begingroup$ I don't follow. Can you show an example? $\endgroup$
    – Szabolcs
    Nov 13 '20 at 11:31
  • 1
    $\begingroup$ Use RandomChoice once and break into subsets? $\endgroup$ Nov 13 '20 at 16:49
  • $\begingroup$ What do you mean by "random sample"? Do you mean a random permutation of elements? $\endgroup$
    – Szabolcs
    Nov 14 '20 at 12:48
  • 3
    $\begingroup$ It sounds like you just want RandomSample[list, 40] . There will be no repetition in the result. Please read the documentation because it sounds like you don't know about the second argument. $\endgroup$
    – flinty
    Nov 14 '20 at 12:51

It seems to me the key to this question is "such that each time I sample I'm sure it chooses a new random sample": RandomSample[] would in principle possibly generate repetitions.

Of course, if you want a method to not produce repetitions, it can't be entirely random because it needs to "remember" which samples have already been generated. A deterministic method generating all different samples exactly once in some arbitrary order would be much more appropriate, but wouldn't qualify as random for most definitions.

Now, for your example of 100 numbers and samples of size 40, it's easy to see that if you just use RandomSample[] the probability of ever seeing a repeated sample is minuscule: consider you have Binomial[100, 40] ~ 10^28 possible choices, so even if you eventually see 10^10 samples you'd have Binomial[10^10, 2] ~ 10^19 pairs to test, thus you expect to have ~10^-9 collisions! So if this is your actual situation just use RandomSample[], because trying to remember which samples you've seen will be useless and might in the worst case lead you to memory issues.

  • $\begingroup$ I think people are confused whether you mean two samples (= two results of RandomSample[..]) should be distinct samples or that in a single sample there should be no repetitions. I think you mean the former. And to clarify {1, 2} and {1, 3} are distinct samples, no? $\endgroup$
    – Michael E2
    Nov 14 '20 at 20:21

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