Picking random elements from a vector but excluding certain numbers each time

Viewed 65

I have this vector

 K=c(1,2,3,4,5,6,8,10,12,14)

I want to pick 2 random elements from K such that my output never includes 6 or 14 or both each time. How can i do this for it to have output like if i used

 S=c(1,2,3,4,5,8,10,12)
 sample(S,2)
1 Answers

You may take 6 and 14 out of the vector of candidates to sample from, as in

sample(setdiff(K, c(6, 14)), 2) 
Related