Skip to content

Commit

Permalink
refactor(Queue): shuffle() using Fisher–Yates algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
hmes98318 committed Sep 15, 2023
1 parent efa3399 commit b317579
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/lib/queue/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ export class Queue {
*/
public shuffle() {
if (this.tracks.length) {
let j;
for (let i = this.tracks.length - 1; i; i--) {
j = Math.floor(Math.random() * (i + 1));

[this.tracks[i], this.tracks[j]] = [this.tracks[j], this.tracks[i]];
const len = this.tracks.length;
for (let i = 0; i < len; i++) {
const rand = Math.floor(Math.random() * (len - i)) + i;
[this.tracks[i], this.tracks[rand]] = [this.tracks[rand], this.tracks[i]];
}
}
}
Expand Down

0 comments on commit b317579

Please sign in to comment.