Lodash _.sampleSize() method
What you’ll learn
- How
_.sampleSize(collection, n)returns up tonpseudo-random elements as a new array. - Behavior when
nexceeds collection size or when the collection is empty. - Differences from single-draw
sampleand fullshufflepermutations. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.sample() first—same RNG caveats, but this helper draws batches instead of singletons.
- You know why helper randomness is not cryptographically secure by default.
- You can open Try-it labs or run snippets locally.
Overview
_.sampleSize packages “give me n winners” logic—lottery seats, spot QA checks, randomized A/B cohort slices—without manually indexing Math.random.
Batch sampling
One call returns an array whose length is min(n, size) for finite collections.
Objects & strings
Sample plain-object values or string characters using the same arity-two signature.
Non-destructive
Outputs are fresh arrays—you never mutate client payloads accidentally.
Syntax
_.sampleSize(collection, n) - collection: array, array-like string, or plain object Lodash can iterate.
- n: non-negative integer count—results shrink automatically when the pool is smaller.
- Returns: new array (possibly empty) of sampled members in arbitrary order.
Draw two numbers from an array
Each Try-it run can permute differently—Lodash picks distinct indexes into the source array.
import sampleSize from "lodash/sampleSize";
sampleSize([1, 2, 3, 4], 2);
// → length 2 array; elements ∈ {1,2,3,4} Draw two values from a plain object
Returned entries are values only—keys participate solely during enumeration.
import sampleSize from "lodash/sampleSize";
sampleSize({ alpha: 1, beta: 2, gamma: 3 }, 2);
// → two distinct values from {1,2,3} Draw three characters from a string
Duplicate letters can appear twice because each index is unique—even when characters match visually.
import sampleSize from "lodash/sampleSize";
sampleSize("hello", 3);
// → array of three single-character strings 📋 _.sampleSize vs sample, shuffle, manual loops
| API | Outcome | Best when |
|---|---|---|
_.sampleSize(collection, n) | Up to n picks | You need multiple unique draws without reordering everything |
_.sample(collection) | Exactly one pick | Singleton lottery spins |
_.shuffle(collection) | Permuted copy | You eventually consume the entire randomized sequence |
| Manual Fisher-Yates slice | Custom RNG control | You must reproduce statistical guarantees outside Lodash |
Pitfalls to avoid
Predictable RNG
Security workloads still demand crypto-grade sources—Lodash stays in UX/random-demo territory.
Expecting sorted results
Results arrive shuffled—sort explicitly when IDs must rise monotonically.
Silent truncation
When n overshoots pool size you simply receive fewer elements—validate counts in business logic.
❓ FAQ
Summary
- Purpose:
_.sampleSize(collection, n)builds a new array with up tonpseudo-random members. - Contrast: prefer
samplefor single draws; planshufflewhen the entire permutation matters. - Next: Lodash _.shuffle(), Lodash _.sample() (previous), or collection hub.
When n is larger than the available finite elements (arrays/objects), Lodash returns only what exists—never padded placeholders—mirroring bounded reservoir intuition rather than throwing.
6 people found this page helpful
