Skip to content

shuffle

Static Badge

接收数组 arr,返回一个随机打乱顺序的新数组。

参数 notFixedPoint 用于控制是否强制所有元素在打乱后离开其原始位置。

  • 若为 false(默认),使用 Fisher-Yates 算法(允许元素可能留在原位)。
  • 若为 true,使用 Sattolo 算法(确保每个元素都不在原位)。

Added in v0.1.0

Usage

ts
import { shuffle } from 'parsnip-kit'

const original = [1, 2, 3, 4, 5]
const res0 = shuffle(original) // It would be [3, 2, 4, 1, 5]
const res1 = shuffle(original, true) // It would be [3, 4, 2, 5, 1]
res0 === original // false
res1 === original // false

API

Type Parameter

ArgTypeDescription
T输入数组的元素类型

Arguments

ArgTypeOptionalDefaultDescription
arrT[]falseundefined待洗牌的输入数组
noFixedPointBooleantruefalse是否确保所有元素均不在原始位置

Returns

Type
T[]