//数组乱序 洗牌算法Fisher–Yates shuffle Array.prototype.shuffle = function() { var input = this; for (var i = input.length-1; i >=0; i--) { var randomIndex = Math.floor(Math.random()*(i+1)); var itemAtIndex = input[randomIndex]; input[randomIndex] = input[i]; input[i] = itemAtIndex; } return input; } //数组排列组合 Array.prototype.permute = function() { var permArr = [], usedChars = []; var input = this; function main(input){ var i, ch; for (i = 0; i < input.length; i++) { ch = input.splice(i, 1)[0]; usedChars.push(ch); if (input.length == 0) { permArr.push(usedChars.slice()); } main(input); input.splice(i, 0, ch); usedChars.pop(); } return permArr } return main(input); }
转载于:https://my.oschina.net/u/232595/blog/1605357