I have spent over a day trying to accomplish what seems to be a very simple thing. I have to create 300 'random' sequences in which the numbers 1,2,3 and 4 all appear exactly 12 times, but the same number is never used twice 'in a row'/consecutively.
My best attempts (I guess) were:
have R sample 48 items without replacement, test whether there are consecutive values with rle, then use only the sequences that do not contain consecutive values. Problem: there are almost no random sequences that meet this criterion, so it takes forever.
have R create sequences without consecutive values (see code).
pop<-rep(1:4,12)
y=c()
while(length(y)!=48)
{
y= c(y,sample(pop,48-length(y),replace=F))
y=y[!c(FALSE, diff(y) == 0)]
}
Problem: this creates sequences with varying numbers of each value. I then tried to use only those sequences with exactly 12 of each value, but that only brought me back to problem 1: takes forever.
There must be some easy way to do this, right? Any help is greatly appreciated!