Created
May 26, 2020 02:44
-
-
Save imbyc/3b4fd26191ce81e703e6a7b773856467 to your computer and use it in GitHub Desktop.
[JS随机打乱数组] https://xugaoyi.com/pages/40b4db2d38ba85f2/
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 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[j] = t | |
} | |
return _arr | |
} | |
function getRandomInt(min, max) { // 获取min到max的一个随机数,包含min和max本身 | |
return Math.floor(Math.random() * (max - min + 1) + min) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment