方法一: function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1) + min) } function shuffle(arr) { let _arr = arr.slice() for (let i = 0; i < _arr.length; i++) { let j = getRandomInt(0, i) let t = _arr[i] _arr[i] = _arr[j] _arr…
let arr = ['g', 'b', 'c', 'd', 'e', 'a', 'g', 'b', 'c']; // 数组随机排序(原数组被修改)Array.prototype.randomSort = function () { const len = this.length; for (let i = len - 1; i > 1; i--) { let n = Math.floor(Math.random() * i); let lastone = this[i]; this[i] =…
其实蛮容易实现的,关键是简洁与否,下面是我自己写的. function randomSort(a){ var arr = a, random = [], len = arr.length; for (var i = 0; i < len; i++) { var index = Math.floor(Math.random()*(len - i)); random.push(a[index]); arr.splice(index,1); } return random; } var a = [1,…