# [R] Generating unordered, with replacement, samples

Duncan Murdoch murdoch.duncan at gmail.com
Wed Sep 17 21:07:22 CEST 2014

```On 17/09/2014 2:25 PM, Giovanni Petris wrote:
> Hello,
>
> I am trying to interface in my teaching some elementary probability with Monte Carlo ideas. In sampling from a finite population, the number of distinct samples of size 'k' from a population of size 'n' , when individuals are selected with replacement and the selection order does not matter, is choose(n + k -1, k). Does anyone have a suggestion about how to simulate (uniformly!) one of these possible samples? In a Monte Carlo framework I would like to do it repeatedly, so efficiency is of some relevance.
>
> Thank you in advance!

I forget the details of the derivation of that count, but the number
suggests it is found by selecting k things without replacement from
n+k-1.  The sample() function in R can easily give you a sample of k
integers from 1:(n+k-1); "all" you need to do is map those numbers into
your original sample of k from n.  For that you need to remember the
derivation of that formula!

Duncan Murdoch

```