TS-Scribe - v0.6.2
    Preparing search index...

    Function randomSample

    • Returns a random sample of unique elements from the given array.

      Type Parameters

      • T

        The type of elements in the input array.

      Parameters

      • array: T[]

        The array to sample from.

      • size: number = 1

        The number of elements to sample. Defaults to 1.

      Returns T[]

      A new array containing size randomly selected unique elements from the input array. If size is greater than the length of the array, the original array is returned. If size is less than 0, an empty array is returned.

      randomSample([1, 2, 3, 4, 5], 2); // might return [3, 1]
      randomSample(['a', 'b', 'c']); // returns one random element, e.g. ['b']