Underscore.js _.random() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Utility

Definition

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

ParameterDescription
minOptional. Lower bound (inclusive). Omitted → treat as 0
maxRequired. Upper bound (inclusive). Alone = max with min 0

Return Value

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

How It Works

  1. Set the range. min is 0, max is 100.
  2. Inclusive ends. Both 0 and 100 are possible.
  3. Docs sample. 42 is one possible result, not a fixed return.
  4. 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
Try It Yourself

How It Works

  1. One number. Underscore reads it as max.
  2. Default min. Lower bound becomes 0.
  3. Same range. Matches _.random(0, 10) in meaning.
  4. Two calls differ. Do not compare two rolls with === as a range test.

Example 3 — Dice Roll (1 to 6)

Inclusive bounds make a six-sided die natural.

Underscore
_.random(1, 6);
// → 1, 2, 3, 4, 5, or 6

var roll = _.random(1, 6);
roll >= 1 && roll <= 6;
// → true
Try It Yourself

How It Works

  1. Set faces. 1 through 6.
  2. Inclusive. Both 1 and 6 can appear.
  3. Six options. Count is 6 - 1 + 1 = 6.
  4. Everyday use. Games, picks, and mock data — not security tokens.

Example 4 — Several Rolls with _.times

Combine with _.times to build a small list of rolls.

Underscore
var rolls = _.times(5, function () {
  return _.random(1, 6);
});
// → e.g. [3, 6, 1, 4, 2]

rolls.length;
// → 5

_.every(rolls, function (n) {
  return n >= 1 && n <= 6;
});
// → true
Try It Yourself

How It Works

  1. Call five times. _.times(5, …) runs the iteratee five times.
  2. Each roll. The iteratee returns _.random(1, 6).
  3. Collect. You get an array of five integers.
  4. Validate. Every value still sits in 1..6.

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 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
_.random Underscore 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.

Next: _.mixin()

Learn how to extend Underscore with your own helper functions.

_.mixin() 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