Skip to content

shuffleInPlace

Static Badge

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 // true

API

Type Parameter

ArgTypeDescription
TType of elements of input array

Arguments

ArgTypeOptionalDefaultDescription
arrT[]falseundefinedInput array to shuffle
noFixedPointBooleantruefalseInput array to shuffle

Returns

Type
T[]