shuffleInPlace
Accepts an array arr and returns a reference to the original array with its elements randomly shuffled.
The parameter notFixedPoint controls whether all elements are forced to leave their original positions after shuffling.
- If
false(default), the Fisher-Yates algorithm is used (elements may stay in their original positions). - If
true, the Sattolo algorithm is used (ensuring that no element remains in its original position).
Added in v0.1.0
Usage
ts
import { shuffleInPlace } from 'parsnip-kit'
const original = [1, 2, 3, 4, 5]
const res0 = shuffleInPlace(original) // It would be [3, 2, 4, 1, 5]
const res1 = shuffleInPlace(original, true) // It would be [3, 4, 2, 5, 1]
res0 === original // true
res1 === original // trueAPI
Type Parameter
| Arg | Type | Description |
|---|---|---|
T | | Type of elements of input array |
Arguments
| Arg | Type | Optional | Default | Description |
|---|---|---|---|---|
arr | T[] | false | undefined | Input array to shuffle |
noFixedPoint | Boolean | true | false | Input array to shuffle |
Returns
| Type |
|---|
T[] |