Lodash _.sampleSize() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Collection

Definition

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])
CallWhat 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

ParameterDescription
collectionRequired. Array or object to sample from
nOptional. How many unique picks; default 1

Return Value

Return ValueDescription
*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)
Try It Yourself

How It Works

  1. Source. Three numbers available.
  2. Ask for 2. Lodash picks two different indexes.
  3. Array result. Always returns an array (not a scalar).
  4. No repeats. The same index is not chosen twice in one call.

Example 2 — Official: n Larger Than Length

When n exceeds the collection, you get every element — not padding.

Lodash
_.sampleSize([1, 2, 3], 4);
// → [2, 3, 1]   (docs example — all three, random order)
Try It Yourself

How It Works

  1. Ask for 4. Only 3 items exist.
  2. Cap. Lodash takes min(4, 3) = 3.
  3. All values. Result length is 3 — a shuffled copy of the source values.
  4. No throw. Oversized n is safe.

Example 3 — Default Size 1

Omit n and you get a one-element array.

Lodash
_.sampleSize([10, 20, 30]);
// → [20]   (example — one of the three, wrapped in an array)
Try It Yourself

How It Works

  1. Default. Missing n means 1.
  2. Still an array. Unlike _.sample, the result is [value].
  3. Need a scalar? Use _.sample(arr) or _.sampleSize(arr, 1)[0].
  4. Empty. _.sampleSize([]) → [].

Example 4 — Sample Several Names

Pick a small unique team from a larger list.

Lodash
_.sampleSize(["Ada", "Lin", "Sam", "Joy", "Kai"], 3);
// → ["Sam", "Ada", "Kai"]   (example — any unique trio)
Try It Yourself

How It Works

  1. Pool. Five names available.
  2. Take 3. Three different indexes are chosen.
  3. Order. Result order is random — not sorted.
  4. Use. Quizzes, demo teams, random quiz questions.

Example 5 — Empty Collection or n = 0

No items to pick → empty array.

Lodash
_.sampleSize([], 2);
// → []

_.sampleSize([1, 2, 3], 0);
// → []
Try It Yourself

How It Works

  1. Empty source. Nothing to choose → [].
  2. Zero size. n = 0 asks for no elements → [].
  3. Safe. No throw; loops over the result simply do nothing.
  4. vs sample. Empty _.sample returns undefined; empty sampleSize returns [].

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.sampleSize Lodash 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.

Next: _.shuffle()

Create a new array with all elements in random order.

_.shuffle() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful