I want to write a function that samples n lottery tickets with 6 numbers each from 1 to 45 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)
Solving without the restriction:
lottery_inef <- function(n){
t(replicate(n,
sample(1:45, 6)))
}
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.
My Idea here would be:
lottery_ef <- function(n){
m <- 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
}
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.