4
$\begingroup$

What is the module that selects a random sample of size k (without replacement) from a given population of size n, involving the command Subsets.

$\endgroup$
9
  • $\begingroup$ I have a problem with " without replacement" $\endgroup$
    – mais
    Nov 25, 2013 at 17:51
  • 1
    $\begingroup$ Enter "random sample", with or without quotes, in the Documentation Center. Check amongst the top hits and you should find something applicable. $\endgroup$ Nov 25, 2013 at 17:53
  • $\begingroup$ I didn't find anything about without replacement $\endgroup$
    – mais
    Nov 25, 2013 at 18:06
  • 3
    $\begingroup$ Click under Details. Always. $\endgroup$ Nov 25, 2013 at 18:25
  • 1
    $\begingroup$ @DanielLichtblau So surely you can answer here! $\endgroup$ Nov 25, 2013 at 19:55

1 Answer 1

6
$\begingroup$

Well, they don't literally use the terminology "without replacement" in the manual, but One might expect you should know that is the same as saying "no elements occur more then once"..

Anyway..in summary there are two related functions:

RandomSample[list,k] -> "without replacement"
RandomChoice[list,k] -> "with replacement"

And to literally answer the question, if you insist on using Subsets,

RandomChoice@Subsets[list,{k}]

will also give a sample "without replacement". Note Subsets approach will be horribly slow for large lists since it generates all combinations then picks one. (Subsets also orders the result which you may or may not want)

Just for fun you get respectable performance out of Subsets if you do this:

Subsets[list,{k},{RandomInteger[ Length[list]! / (Length[list]-k)! / k!]}]
$\endgroup$

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