Skip to content

Commit

Permalink
fix: only pick from the unshuffled portion
Browse files Browse the repository at this point in the history
  • Loading branch information
crishoj committed Jan 9, 2025
1 parent fd3ddea commit 16fec8e
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/random/shuffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ export function shuffle<T>(
random: (min: number, max: number) => number = _.random,
): T[] {
const newArray = array.slice()
for (let idx = 0, randomIdx: number, item: T; idx < array.length; idx++) {
randomIdx = random(0, array.length - 1)
item = newArray[randomIdx]
newArray[randomIdx] = newArray[idx]
newArray[idx] = item
for (let idx = array.length - 1, randomIdx: number, item: T; idx > 0; idx--) {
randomIdx = random(0, idx)
item = newArray[idx]
newArray[idx] = newArray[randomIdx]
newArray[randomIdx] = item
}
return newArray
}

0 comments on commit 16fec8e

Please sign in to comment.