强调一下下
functionshuffle(arr){
return arr.sort(function(){returnMath.random() -0.5;});
}
这种是不完全随机的,参考http://blog.csdn.net/jiang_zzz/article/details/53786999
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
es6写法
Array.prototype.shuffle = function() {
let m = this.length, i;
while (m) {
i = (Math.random() * m--) >>> 0;
[this[m], this[i]] = [this[i], this[m]]
}
return this;
}