-
-
Save allexysleeps/3d1dc21680938f5327555079576c030e to your computer and use it in GitHub Desktop.
shuffle list js
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
const getRandNum = (range) => Math.floor(Math.random() * range) | |
const shuffleList = (list) => { | |
const len = list.length | |
for (let i = 0; i < len; i++) { | |
const idx = getRandNum(len - i) | |
const last = len - i - 1 | |
const tmp = list[last] | |
list[last] = list[idx] | |
list[idx] = tmp | |
} | |
return list | |
} | |
const genList = (n) => { | |
const list = [] | |
for (let i = 0; i < n; i++) { | |
list[i] = i | |
} | |
return list | |
} | |
console.log(genList(100)) | |
console.log(shuffleList(genList(100))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment