Skip to content

Commit

Permalink
docs(shuffle): add document about optional random function
Browse files Browse the repository at this point in the history
  • Loading branch information
MarlonPassos-git authored and aleclarson committed Jan 5, 2025
1 parent 6183485 commit 503b076
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/random/shuffle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ const fish = [

_.shuffle(fish)
```

You can provide a custom random function to make the shuffle more or less random. The custom random function takes minimum and maximum values and returns a random number between them.

```ts
const array = [1, 2, 3, 4, 5]
const customRandom = (min, max) =>
Math.floor(Math.random() * (max - min + 1)) + min
_.shuffle(array, customRandom)
```
17 changes: 17 additions & 0 deletions src/random/shuffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,24 @@ import * as _ from 'radashi'
* @version 12.1.0
*/
export function shuffle<T>(
/**
* The array to shuffle.
*/
array: readonly T[],
/**
* You can provide a custom random function to make the shuffle more or less
* random. The custom random function takes minimum and maximum values and
* returns a random number between them.
*
* @default _.random
* @example
*
* ```ts
* const array = [1, 2, 3, 4, 5]
* const customRandom = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min
* _.shuffle(array, customRandom)
* ```
*/
random: (min: number, max: number) => number = _.random,
): T[] {
const newArray = array.slice()
Expand Down

0 comments on commit 503b076

Please sign in to comment.