🐛 (shuffle) better shuffle array

This commit is contained in:
Julien Calixte
2023-07-24 22:45:02 +02:00
parent 5ac0e33df0
commit 37119b9d1a

View File

@@ -1,14 +1,17 @@
export const shuffleArray = <T>(array: T[]) => { export const shuffleArray = <T>(array: T[]) => {
const arrayCopy = [...array] let currentIndex = array.length,
randomIndex
for (let i = array.length - 1; i > 0; i--) { while (currentIndex !== 0) {
const j = Math.floor(Math.random() * (i + 1)) randomIndex = Math.floor(Math.random() * currentIndex)
const temp = array[i] currentIndex--
array[i] = array[j] ;[array[currentIndex], array[randomIndex]] = [
array[j] = temp array[randomIndex],
array[currentIndex]
]
} }
return arrayCopy return array
} }
export const popNElement = <T>(array: T[], numberOfElements: number) => { export const popNElement = <T>(array: T[], numberOfElements: number) => {