Lodash _.random() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Number

Definition

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

ParameterDescription
lowerOptional. Inclusive lower bound (default 0)
upperOptional. Inclusive upper bound (default 1)
floatingOptional. Force a floating-point result

Return Value

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

How It Works

  1. Call. _.random(0, 5) five times.
  2. Range. Each result is an integer from 0 to 5.
  3. Type. Number.isInteger confirms integers.
  4. Docs. Matches the official first example.

Example 2 — Official: One-Argument Form

A single number means “from 0 to that number.”

Lodash
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
Try It Yourself

How It Works

  1. One arg. _.random(5) sets upper to 5, lower to 0.
  2. Same range. Equivalent to _.random(0, 5).
  3. Integer. Both bounds are integers → integer result.
  4. Docs. Matches the official second example.

Example 3 — Official: Floating Flag

Pass true to force a float from integer-looking bounds.

Lodash
var n = _.random(5, true);

console.log(n); // e.g. 2.847...
console.log(Number.isInteger(n)); // false
Try It Yourself

How It Works

  1. Flag. Second arg true means floating mode.
  2. Range. Value lies between 0 and 5.
  3. Type. Not an integer.
  4. Docs. Matches the official third example.

Example 4 — Official: Float Bounds

If either bound is a float, the result is a float.

Lodash
var n = _.random(1.2, 5.2);

console.log(n); // e.g. 3.41...
console.log(n >= 1.2 && n <= 5.2); // true
console.log(Number.isInteger(n)); // false
Try It Yourself

How It Works

  1. Bounds. 1.2 and 5.2 are floats.
  2. Result. Lodash returns a floating-point number.
  3. Check. Value stays within the range.
  4. Docs. Matches the official fourth example.

Example 5 — Dice Roll (1–6)

A classic inclusive integer use case.

Lodash
function rollDie() {
  return _.random(1, 6);
}

console.log(rollDie()); // 1..6
console.log(rollDie()); // 1..6
console.log(_.inRange(rollDie(), 1, 7)); // true (end exclusive)
Try It Yourself

How It Works

  1. Inclusive. 1 and 6 are both possible.
  2. Helper. rollDie wraps one call.
  3. Check. _.inRange(n, 1, 7) confirms 1..6.
  4. Use. Games, demos, shuffled picks — not secrets.

Browser & Runtime Support

_.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 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 Since 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.

Next: Object methods

Explore Lodash helpers for working with objects.

Object methods hub →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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