Skip to content

Instantly share code, notes, and snippets.

@allexysleeps
Created January 30, 2020 11:21
Show Gist options
  • Save allexysleeps/3d1dc21680938f5327555079576c030e to your computer and use it in GitHub Desktop.
Save allexysleeps/3d1dc21680938f5327555079576c030e to your computer and use it in GitHub Desktop.
shuffle list js
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