Last active
April 14, 2017 22:47
-
-
Save FergusInLondon/05663ca189046017145500ab2a1e4aeb to your computer and use it in GitHub Desktop.
Simple array shuffler example.
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
/** | |
* shuffle(array[]) - Shuffles an array via a simplistic | |
* algo, not too dissimilar to Fisher-Yates. | |
* | |
* Ignore the -ahem- "clever" variable swapping one-liner, | |
* that's all it does. | |
* | |
* @param array | |
* @return array | |
*/ | |
var shuffle = function(list) { | |
var rand = 0, len = list.length; | |
for( var i = 0; i < len; i++ ) { | |
// rand must be: (i < rand < len) | |
rand = Math.floor(Math.random() * (len - i)) + i; | |
[list[rand], list[i]] = [list[i], list[rand]] | |
} | |
return list; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment