JS Randomly Shuffle an Array
# JS Randomly Shuffle an Array
function shuffle(arr) { // Randomly shuffle an array
let _arr = arr.slice() // Work on a copy of the array, without modifying the original
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) { // Get a random integer between min and max, inclusive
return Math.floor(Math.random() * (max - min + 1) + min)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
Edit (opens new window)
Last Updated: 2026/03/21, 12:14:36