Pick Random

Pick a specified number of random items from an array without repeating.

CategoryRandom
AuthorPolgubau
Since1.0.4
Last Updated2025-03-17
TypeSync
CompatibilityUniversal

Example

  • Input

    pickRandom([1, 2, 3, 4, 5], 3)

    Output:

    [2, 4, 5] // (random items, could be any combination from the array)
  • Input

    pickRandom(['apple', 'banana', 'cherry', 'date'], 2)

    Output:

    ["banana", "cherry"] // (random items, could be any combination from the array)

Return

An array containing the randomly selected items.

Type: T[]

Props

NameTypeDefaultRequiredDescription
arrayT[]nullNoThe input array to pick random items from.
countnumbernullNoThe number of random items to pick.

Notes

This function returns an empty array if the count is less than or equal to zero, or if the input array is empty.

Tags

  • random
  • array
  • utilities

Related