Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javacript: Is there a function that shuffles an array with every element switching places?

I’m looking for a function that, instead of simply shuffling an array, shuffles it without any element being left at the index he was previously in.

I’ve tried the Fisher-Yates algorithm, but it didn’t solve my problem:

function shuffle(array) {
  var m = array.length, t, i;
  while (m) {
    i = Math.floor(Math.random() * m--);
    t = array[m];
    array[m] = array[i];
    array[i] = t;
  }
}

When testing, I’ve got results like [0, 1, 2, 3, 4, 5] shuffling into [5, 0, 3, 1, 4, 2]. But here, 4 pretty much "stayed" at the same index it previously was.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The function I’m looking for would for example randomize [0, 1, 2, 3, 4, 5] into [4, 1, 5, 3, 2] where no element is at the same index it were previously

>Solution :

Check that the element being put into the rightmost (first unsorted) position isn’t the same as what was there originally. You’ll also need to make sure that what gets inserted on the final swap doesn’t result in an infinite loop – use a separate condition for that after the loop is over.

function shuffle(array) {
  const originalArray = [...array];
  let m = array.length;
  while (m > 1) {
    let i;
    do {
      i = Math.floor(Math.random() * m);
    } while (array[i] === originalArray[m - 1]);
    m--;
    const t = array[m];
    array[m] = array[i];
    array[i] = t;
  }
  if (array[0] === originalArray[0]) {
    [array[0], [array[1]]] = [array[1], [array[0]]];
  }
  return array;
}
for (let i = 0; i < 100; i++) {
  console.log(shuffle([0, 1, 2, 3, 4, 5]));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading