Random sequence from fixed ensemble that contains at least one of each character
I am trying to generate a random sequence from a fixed number of characters that contains at least one of each character. For example having the ensemble m = letters[1:3] I would like to create a sequence of N = 10 elements that contain at least one of each m characters, like a a a a b c c c c a I tried with sample(n,N,replace=T) but in this way also a sequence like a a a a a c c c c a can be generated that does not contain b . f <- function(x, n){ sample(c(x, sample(m, n-length(x), replace=TRUE))) } f(letters[1:3], 5) # [1] "a" "c" "a" "b" "a" f(letters[1:3], 5) # [1] "a" "a" "b" "b" "c" f