title: R: how to sample without replacement AND without consecutive same values
body: <p>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.</p>
  
  <p>My best attempts (I guess) were:</p>
  
  <ol>
  <li><p>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.</p></li>
  <li><p>have R create sequences without consecutive values (see code).</p></li>
  </ol>
  
  <pre><code>pop&lt;-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)]
    }
  </code></pre>
  
  <p>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.</p>
  
  <p>There must be some easy way to do this, right? Any help is greatly appreciated!</p>