@supuwoerc/toolkit
    Preparing search index...

    Function sample

    • 从数组中随机抽取指定数量的元素(可以选择是否允许重复) Randomly selects a specified number of elements from an array (chooses with/without replacement)

      Type Parameters

      • T

      Parameters

      • array: T[]

        源数组 / Source array

      • quantity: number

        需要抽取的元素数量 / Number of elements to sample

      • allowReplacement: boolean = false

        是否允许重复抽取,默认为false / Whether to allow replacement, defaults to false

      Returns T[]

      包含随机抽取元素的数组 / Array containing randomly sampled elements

      // 默认不允许重复
      // No replacement by default
      sample([1, 2, 3, 4, 5], 3)

      // 显式指定允许重复
      // Explicitly allow replacement
      sample([1, 2, 3, 4, 5], 3, true)