I stumbled upon the algorithm below for shuffling an array in JavaScript. It seems to have a unique twist compared to the traditional Fisher–Yates shuffle method. This algorithm allows for a wider range of "swaps" as the for-loop counter increases, which is the opposite behavior of the classic Fisher-Yates approach. I'm intrigued by the validity of this algorithm. Could it possibly be a disguised version of Fisher-Yates? Is there any bias involved?
If someone could provide code to test the distribution of permutations generated by this algorithm, that would be greatly appreciated.
<script>
var shuffle = function (myArray) {
var random = 0;
var temp = 0;
console.log(myArray);
for (i = 1; i < myArray.length; i++) {
random = Math.round(Math.random() * i);
console.log(random + '\n');
temp = myArray[i];
myArray[i] = myArray[random];
myArray[random] = temp;
console.log(myArray);
}
return myArray;
}
var arr = [1, 2, 3, 4];
shuffle(arr);
</script>