sample(combn(c(2,4,6,8),2,mean),6,replace = FALSE)
#> [1] 4 5 7 6 3 5
Created on 2020-09-20 by the reprex package (v0.3.0.9001)
is guaranteed to produce all combinations of the four objects taken two at a time.
The OP for loop stops when i reaches length(y), which equals 4.
sample is not guaranteed to exhaust the combination, although it likely will with this few, in any finite number of iterations, so this is a stopping problem, also.
What's needed is a sufficiently large number of iterations to have a good chance of exhausting the sample space, but it guaranteed to complete. (Of course if it exhausts the sample space on one run, it does not necessarily exhaust it on the next.)
y <- c(2,4,6,8)
run_cases <- function(x) {
sample(combn(y,2,mean),6,replace = FALSE) %in% sample(combn(y,2,mean),x,replace = TRUE)
}
run_cases(10)
#> [1] FALSE TRUE TRUE TRUE TRUE TRUE
run_cases(10)
#> [1] TRUE FALSE TRUE TRUE TRUE TRUE
run_cases(10)
#> [1] TRUE TRUE TRUE TRUE TRUE TRUE
Created on 2020-09-20 by the reprex package (v0.3.0.9001)