Skip to content

Instantly share code, notes, and snippets.

@strycore
Created February 7, 2017 06:55
Show Gist options
  • Save strycore/1f39afddb9b5d5cd9c20bc81acc46492 to your computer and use it in GitHub Desktop.
Save strycore/1f39afddb9b5d5cd9c20bc81acc46492 to your computer and use it in GitHub Desktop.
Shuffle
function getRandom(arr, lastIndex) {
var index, size, distance;
size = arr.length;
do {
index = parseInt(Math.random() * size);
distance = Math.abs(index - lastIndex);
} while(size > 5 && distance < size / 3);
return index;
}
var originalArray = [];
for (i=0; i < 100; i++) {
originalArray[i] = i;
}
var arrayCopy = originalArray.slice(0);
var lastIndex = arrayCopy.length;
var elem;
while (arrayCopy.length > 0) {
index = getRandom(arrayCopy, lastIndex);
elem = arrayCopy.splice(index, 1);
lastIndex = index;
console.log(elem);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment