Created
February 7, 2017 06:55
-
-
Save strycore/1f39afddb9b5d5cd9c20bc81acc46492 to your computer and use it in GitHub Desktop.
Shuffle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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