随机数组方法概括

mac2022-06-30  29

1、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; }[1, 2, 3, 4, 5, 6, 7].shuffle();2、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; }3、function RandomSort (a,b){ return (0.5 - Math.random()); }

转载于:https://www.cnblogs.com/ckf1988/p/5728287.html

最新回复(0)