Lodash _.random() method
What you’ll learn
- Argument shapes: no args, one upper bound, two inclusive integer bounds, and the
floatingflag. - When lodash returns an integer versus a fraction (float bounds or
floating: true). - Why
_.randomis not a security primitive—and what to use instead. - Try each lab in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Know that Math.random() returns a float in [0, 1) and is not cryptographically secure. Optional: read _.inRange() for interval tests that pair well with picking random indices.
- Inclusive integer ranges: both endpoints count when lodash chooses an integer.
- Modules:
import random from "lodash/random"keeps bundles small.
Overview
_.random wraps Math.random with friendlier overloads: coin-flip with no args, integer dice rolls with two integer bounds, and optional floating results when you need jitter instead of grid-aligned picks.
Dice & indexes
Two integer arguments yield an inclusive integer—ideal for arr[_.random(arr.length - 1)] when length is positive.
Float mode
Pass true as the third argument or use float bounds to get fractional values in the span.
Not for secrets
Predictable PRNG only—never generate API keys or passwords with this helper.
Syntax
_.random([lower = 0], upper, [floating]) - No arguments: returns
0or1(integers). - One number: upper bound; result is an integer from
0through that bound inclusive. - Two numbers: lower and upper (order can be reversed; lodash normalizes). Integers + falsy
floating→ inclusive integer. Float bound orfloating === true→ floating-point result. - floating: optional third argument; when truthy, forces a floating result between the ordered bounds.
Integer overloads
No arguments is a coin flip. One argument spans 0..n. Two integer arguments span that inclusive range (lodash swaps if you pass the larger number first).
import random from "lodash/random";
random(); // 0 or 1
random(5); // integer in [0, 5]
random(1, 6); // integer in [1, 6] (dice) Floating results
Pass true as the third argument for a float between two integers, or use float bounds—lodash switches to floating mode automatically when either endpoint is non-integer.
import random from "lodash/random";
random(5, 10, true); // float in [5, 10]
random(1.5, 2.5); // float; bounds already fractional Degenerate range & security note
When lower === upper, lodash returns that constant. For anything sensitive, skip lodash and use cryptographic randomness.
import random from "lodash/random";
random(7, 7); // => 7
// Tokens / keys — use crypto instead, e.g. in the browser:
// const bytes = new Uint8Array(16);
// crypto.getRandomValues(bytes); 📋 _.random vs Math.random
| Goal | Math.random() | _.random(...) |
|---|---|---|
Float in [0, 1) | Built in | Not the default; use two floats or floating: true on an interval |
Integer dice 1..6 | Math.floor(Math.random() * 6) + 1 | random(1, 6) |
Coin flip 0|1 | Math.floor(Math.random() * 2) or Math.round(Math.random()) | random() in one call |
Lodash focuses on bounded integers and optional floats; Math.random stays the primitive for raw [0, 1) sampling.
Pitfalls to avoid
Predictable output
Attackers model Math.random. Never issue passwords, OTP seeds, or lottery winners that matter with _.random.
Index length - 1
random(arr.length - 1) becomes random(-1) when length is 0. Guard length first.
Accidental float bounds
Mixing in a float (for example from division) flips the helper to floating mode—watch types when you expected an integer index.
❓ FAQ
Summary
- Purpose: bounded random integers or floats built on
Math.random. - Remember: integer ranges are inclusive; floats appear when forced by flag or fractional bounds.
- Next: Lodash Object methods, _.inRange(), or the official Lodash docs for _.random.
_.random delegates to Math.random internally—fine for games and demos, not for secrets. When both bounds are integers and floating is falsy, the result is an integer in the inclusive range; if either bound is a float or floating is true, you get a floating-point value.
6 people found this page helpful
