I came across the algorithm below for shuffling an array in Javascript. It seems to differ from the Fisher–Yates shuffle in that the range of available "swaps" increases with the for-loop counter. This appears to be the opposite of how the Fisher-Yates version behaves. I'm curious as to whether this is a valid algorithm. Is it the Fisher-Yates in disguise? Is it biased?
If anyone could provide some code to test the frequency of the permutations it generates that would be a bonus.
<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>