Skip to content

Commit

Permalink
added shuffle
Browse files Browse the repository at this point in the history
  • Loading branch information
j50n committed Oct 3, 2023
1 parent f430abe commit 3cac380
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,15 @@ export async function sleep(delayms: number): Promise<void> {
export function isString(s: unknown): s is string {
return Object.prototype.toString.call(s) === "[object String]";
}

/**
* Perfect in-place array shuffle in linear time.
*/
export function shuffle<T>(items: T[]) {
for (let i = 0; i < items.length; i++) {
const j = Math.floor(Math.random() * items.length);
const tmp = items[i];
items[i] = items[j];
items[j] = tmp;
}
}
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"0.20.43"}
{"version":"0.20.44"}

0 comments on commit 3cac380

Please sign in to comment.