title: Efficiently Sampling Lottery Numbers in R
body: <p>I want to write a function that samples <strong>n</strong> lottery tickets with <strong>6</strong> numbers each from <strong>1 to 45</strong> without replacement. However, I need to do this efficiently, meaning no loops or loop-like functions. (I guess Rcpp would also work but I would prefer a vectorized solution in base R)</p>
  <p>Solving without the restriction:</p>
  <pre><code>lottery_inef &lt;- function(n){
    
   t(replicate(n,
            sample(1:45, 6)))
  }
  </code></pre>
  <p>So here I get a matrix where each row corresponds to one lottery ticket. Now if I want to simulate millions of lottery tickets this gets pretty slow and hence I am interested in a vectorized solution.</p>
  <p>My Idea here would be:</p>
  <pre><code>lottery_ef &lt;- function(n){
    
    m &lt;- matrix(sample(1:45, n*6, replace = TRUE), ncol = 6)
    
    # somehow subset the matrix without a loop to remove all the 
    # rows that have non-unique values as in the lottery we can only draw each number once
  }
  </code></pre>
  <p>For the efficient version, I am sort of lost at the point of subsetting without a loop or apply(). If anyone can solve this subsetting problem or point me in a completely different direction that will lead me to a solution I would be very grateful.</p>