Skip to content

Instantly share code, notes, and snippets.

@gcpantazis
Created June 4, 2012 01:18
Show Gist options
  • Save gcpantazis/2865745 to your computer and use it in GitHub Desktop.
Save gcpantazis/2865745 to your computer and use it in GitHub Desktop.
Array Shuffler [Randomly Efficient]
var y = [25, 8, 7, 41, 22, 11, 2, 1000];
var randomize = function(array) {
var output = [],
arrLen = array.length;
for ( var i = 0; i < array.length; i++ ) {
output[i] = false;
}
var findSpot = function(inputVal){
var potentialSpot = Math.floor(Math.random()*arrLen);
if ( !output[potentialSpot] ) {
output[potentialSpot] = inputVal;
} else {
findSpot(inputVal);
}
}
for ( var i = 0; i < array.length; i++ ) {
findSpot(array[i]);
}
return output;
}
randomize(y);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment