Gets n random elements at unique keys from a collection, up to the size of the collection. Default n is 1. Part of Lodash 4.x Collection (since 4.0.0).
Pass the required arguments shown below. Lodash Collection helpers usually return a new value — check whether the source is mutated.
Syntax
Call _.sampleSize on a collection. Start with this line.
Lodash
_.sampleSize(collection, [n=1])
Call
What it does
_.sampleSize(collection, [n=1])
Primary signature.
_.sampleSize(collection)
Call with a collection argument.
Prefer import sampleSize from "lodash/sampleSize" when you want a smaller bundle.
Library declarations
Lodash
_.sampleSize(collection, [n=1])
Parameters
Parameter
Description
collection
Required. Array or object to sample from
n
Optional. How many unique picks; default 1
Return Value
Return Value
Description
*
New array of sampled elements
Lodash Examples
Example 1 — Official: Sample Two
Ask for two unique numbers from three.
Lodash
_.sampleSize([1, 2, 3], 2);
// → [3, 1] (docs example; order and pair may differ)
Safe. No throw; loops over the result simply do nothing.
vs sample. Empty _.sample returns undefined; empty sampleSize returns [].
Compatibility
Browser & Runtime Support
_.sampleSize is a Lodash 4.x Collection helper (since 4.0.0). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash.
✓ Stable · Lodash 4.0.0+
_.sampleSize()
Available in [email protected] full builds and the lodash/sampleSize modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.
100%With Lodash 4.x
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.sampleSizeLodash 4.x+
Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import sampleSize from "lodash/sampleSize" for smaller builds. Not a native browser API — load Lodash first.
Frequently Asked Questions
_.sampleSize(collection, [n=1]) returns an array of up to n random elements from unique positions — sampling without replacement. It does not mutate the source.
Lodash returns at most collection.length elements — never pads. Example: sampleSize([1,2,3], 4) returns three values in random order.
_.sample returns one value. _.sampleSize returns an array of unique picks. sampleSize(coll, 1) is similar to [_.sample(coll)] when the collection is non-empty.
_.shuffle reorders the whole collection. sampleSize asks for only n elements — cheaper when n is small.
No. It uses Lodash's pseudo-random helper, not crypto.getRandomValues. Do not use it for tokens, lotteries, or security-sensitive picks.
Use import sampleSize from "lodash/sampleSize" or require("lodash/sampleSize") for tree-shaking. Full build: import _ from "lodash" then _.sampleSize.