title: sample array n^2 times without the same number occurring twice in a row and not repeating every n
body: <p>I'm trying to write a function that has the arguments of the array to sample <code>arr</code> and the number of samples, <code>size</code> (which is sqaured) and randomly samples from the original array:</p>
  <pre><code>arr = [1,2,3,4]
  
  single_number = (x) =&gt; {
    return x[Math.floor(Math.random()*x.length)];
  }
  
  randomize = (arr, size) =&gt; {
    return Array(size*size).fill().map(x =&gt; single_number(arr))
  }
  
  randomize(arr, 5)
  </code></pre>
  <p>I want to add the additional requirements to my randomize function:</p>
  <ol>
  <li>no number shows up twice in a row</li>
  <li>make sure every <code>size</code>th item is not the same as the one before it</li>
  </ol>
  <p>For example</p>
  <p><code>randomize([1,2,3,4], 2)</code></p>
  <pre><code>[2,4,3,2,4,1,1,2,2,1,4,1,1,1,3,1,1,4,4,1,3,3,2,2,3]
  </code></pre>
  <p>CASE (1)</p>
  <pre><code>[
    2,4,3,2,4,
    2,1,       
    2,2,       // illegal!
    1,4,
    1,1,1,     // illegal!
    3,
    1,1,       // illegal!
    4,4,       // illegal!
    1,
    3,3,       // illegal!
    2,2,       // illegal!
    3
  ]
  </code></pre>
  <p>CASE (2)</p>
  <pre><code>[
    2,4,3,2,4,  [0] === 2 
    2,1,2,2,1,  [5] === 2 // illegal! 
    4,1,1,1,3,  
    1,1,4,4,1,
    3,3,2,2,3
  ]
  </code></pre>
  <p>I'm trying to use functional programming and avoid a for loop if possible since I think I can do this with a nested for loop?</p>