Created
October 28, 2017 10:33
-
-
Save tex2e/cfb8698208b724eb53bba70f43098343 to your computer and use it in GitHub Desktop.
shuffle array
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 rand(min, max) { | |
if (max === undefined) { max = min; min = 0; } | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
function shuffle(array) { | |
var sample = array.slice(0); | |
var length = sample.length; | |
var last = length - 1; | |
for (var index = 0; index < length; index++) { | |
var rand = Utils.rand(index, last); | |
var temp = sample[index]; | |
sample[index] = sample[rand]; | |
sample[rand] = temp; | |
} | |
return sample; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment