Produces a random number between the inclusive lower and upper bounds. Part of Lodash Number (stable since 0.7.0 — not deprecated). One argument → 0 to that number. If floating is true, or either bound is a float, you get a floating-point result. Note: JavaScript follows IEEE-754, so float results can look unexpected.
Pass the numbers shown below. Check bounds and return type before you use the result.
Syntax
Call _.random. Start with this line.
Lodash
_.random([lower=0], [upper=1], [floating])
Call
What it does
_.random([lower=0], [upper=1], [floating])
Primary signature.
Prefer import random from "lodash/random" when you want a smaller bundle.
Library declarations
Lodash
_.random([lower=0], [upper=1], [floating])
Parameters
Parameter
Description
lower
Optional. Inclusive lower bound (default 0)
upper
Optional. Inclusive upper bound (default 1)
floating
Optional. Force a floating-point result
Return Value
Return Value
Description
*
The random number
Lodash Examples
Example 1 — Official: Inclusive Integer Range
Both bounds are inclusive integers.
Lodash
var samples = [];
for (var i = 0; i < 5; i++) {
samples.push(_.random(0, 5));
}
console.log(samples.join(", "));
console.log(Number.isInteger(samples[0])); // true
var a = _.random(5);
var b = _.random(0, 5);
console.log(a); // integer 0..5
console.log(b); // also integer 0..5
console.log(Number.isInteger(a)); // true
_.random is a Lodash Number helper available since 0.7.0. It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. It depends on the host Math.random implementation.
✓ Stable · Lodash 4.x
_.random()
Available in [email protected] full builds and the lodash/random 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
_.randomSince 0.7.0
Bottom line: Safe for games and demos once Lodash is on the page. Prefer import random from "lodash/random" for smaller builds. Do not use for cryptographic secrets.
Frequently Asked Questions
_.random([lower=0], [upper=1], [floating]) produces a random number between the inclusive lower and upper bounds. Official: _.random(0, 5) → an integer between 0 and 5.
Yes. Both lower and upper are inclusive for integers — _.random(1, 6) can return 1 or 6.
When floating is true, or when either bound is already a float. Otherwise two integer bounds yield an integer.
One numeric argument is treated as upper with lower = 0. So _.random(5) is also an integer between 0 and 5.
No. It uses Math.random under the hood — fine for games and demos, not for tokens or keys. Use the Web Crypto API or Node crypto instead.
Use import random from "lodash/random" or require("lodash/random") so bundlers can tree-shake. Full build: import _ from "lodash" then _.random.