Returns a random integer between min and max, inclusive. With one argument, returns a random integer between 0 and that number, inclusive (official Underscore docs).
Pass the required arguments shown below. Underscore utility helpers usually return a new value or function — check edge cases before you ship.
Syntax
Call _.random. Start with this line.
Underscore
_.random([min], max)
Call
What it does
_.random([min], max)
Primary signature.
_.random(...)
Call with the arguments your case needs.
Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.
Library declarations
Underscore
_.random([min], max)
Parameters
Parameter
Description
min
Optional. Lower bound (inclusive). Omitted → treat as 0
max
Required. Upper bound (inclusive). Alone = max with min 0
Return Value
Return Value
Description
*
Integer in the inclusive range
Underscore.js Examples
Example 1 — Official _.random(0, 100)
Any integer from 0 through 100. The docs sample is 42 — your run will differ.
Underscore
_.random(0, 100);
// → 42 (docs sample — each run differs)
var n = _.random(0, 100);
n >= 0 && n <= 100;
// → true
Docs sample.42 is one possible result, not a fixed return.
Check. Any live value still satisfies 0 ≤ n ≤ 100.
Example 2 — One-Argument Form
Pass only max. Underscore treats min as 0.
Underscore
_.random(10);
// → integer in [0, 10]
_.random(10) === _.random(0, 10);
// → maybe true or false (two separate rolls)
var n = _.random(10);
n >= 0 && n <= 10;
// → true
Call five times._.times(5, …) runs the iteratee five times.
Each roll. The iteratee returns _.random(1, 6).
Collect. You get an array of five integers.
Validate. Every value still sits in 1..6.
Compatibility
Browser & Runtime Support
_.random ships with Underscore.js (1.4.0+). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore. Relies on Math.random.
✓ Stable · Underscore 1.4+
_.random()
Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.
100%With Underscore 1.4+
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
_.randomUnderscore 1.4+
Bottom line: Safe for everyday non-security use once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).
Frequently Asked Questions
_.random(min, max) returns a random integer between min and max, inclusive. If you pass only one argument, it returns an integer between 0 and that number, inclusive.
_.random(0, 100) can return any integer from 0 through 100. The docs show 42 as a sample — each run is different.
Yes. _.random(1, 6) can be 1 and can be 6. That makes it a natural helper for dice and other inclusive integer ranges.
Math.random() returns a float in [0, 1). _.random() returns an integer in an inclusive range. Underscore has no floating flag — use Math.random when you need a fraction.
No. It uses Math.random internally. Do not use it for tokens, passwords, or session IDs. Use crypto.getRandomValues in the browser or crypto.randomBytes in Node.js.
No. It is a stable Underscore utility added in version 1.4.0 to return a random integer in a given range.