Fisher-Yates (aka Knuth) Shuffle

by mtambulut

JavaScript

var arr = [1,2,3,4,5,6,7,8,11,22,33,78,97,99,98];
arr = shuffle(arr);
console.log(arr);

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}
//Diziyi random karıştıran(suffle) fonksıyon
//https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle