Created
June 4, 2012 01:18
-
-
Save gcpantazis/2865745 to your computer and use it in GitHub Desktop.
Array Shuffler [Randomly Efficient]
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
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