Skip to content

Commit

Permalink
added u.shuffle
Browse files Browse the repository at this point in the history
  • Loading branch information
Emrio committed Sep 23, 2021
1 parent 1245f40 commit 476f544
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/_exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export * from './resolveObjectProperty'
export * from './rmfast'
export * from './round'
export * from './setmanip'
export * from './shuffle'
export * from './sleep'
export * from './splice'
export * from './split'
Expand Down
14 changes: 14 additions & 0 deletions src/shuffle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { randint } from './randint'
import { randUniqueList } from './randUniqueList'

/**
* Shuffle elements in an array
*/
export function shuffle <T> (arr: T[]): void {
const indices = randUniqueList(() => randint(arr.length), arr.length)
const values = indices.map(i => arr[i])

for (let i = 0; i < arr.length; i++) {
arr[i] = values[i]
}
}

0 comments on commit 476f544

Please sign in to comment.