Returns a random subset of items without mutating the source array.
items
Defaults to count = 1. Returns [] when count is 0, and all items when count equals the array length.
count = 1
[]
count
0
Source array; not mutated.
Number of items to take after shuffling. Defaults to 1; 0 returns [], and values equal to items.length return all items.
1
items.length
A new array of up to count items in random order (via randomShuffle).
const items = ["a", "b", "c", "d"];randomSample(items, 2); // e.g. ["d", "a"]randomSample(items); // e.g. ["b"] (default count: 1)randomSample(items, 0); // []randomSample(items, items.length); // all items, shuffled Copy
const items = ["a", "b", "c", "d"];randomSample(items, 2); // e.g. ["d", "a"]randomSample(items); // e.g. ["b"] (default count: 1)randomSample(items, 0); // []randomSample(items, items.length); // all items, shuffled
Returns a random subset of
itemswithout mutating the source array.Defaults to
count = 1. Returns[]whencountis0, and all items whencountequals the array length.