sampleSize es-toolkit
Returns a new array of randomly selected elements with the specified size.
Usage
sampleSize(array, size)
Use sampleSize when you want to randomly sample multiple elements from an array. It uses Floyd's algorithm to efficiently generate random samples without duplicates. It's useful for extracting samples in surveys or randomly selecting multiple items in games.
You can sample with various sizes.
Parameters
The array to sample from.
The number of elements to select.
Returns
Returns a new array consisting of randomly selected elements.
Throws
Throws an error if size is greater than the length of the array.
Source: es-toolkit
Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.