Lodash _.random() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • Argument shapes: no args, one upper bound, two inclusive integer bounds, and the floating flag.
  • When lodash returns an integer versus a fraction (float bounds or floating: true).
  • Why _.random is 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

javascript
_.random([lower = 0], upper, [floating])
  • No arguments: returns 0 or 1 (integers).
  • One number: upper bound; result is an integer from 0 through that bound inclusive.
  • Two numbers: lower and upper (order can be reversed; lodash normalizes). Integers + falsy floating → inclusive integer. Float bound or floating === true → floating-point result.
  • floating: optional third argument; when truthy, forces a floating result between the ordered bounds.
1

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

javascript
import random from "lodash/random";

random();       // 0 or 1

random(5);      // integer in [0, 5]

random(1, 6);   // integer in [1, 6] (dice)
Try it Yourself
2

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.

javascript
import random from "lodash/random";

random(5, 10, true);   // float in [5, 10]

random(1.5, 2.5);      // float; bounds already fractional
Try it Yourself
3

Degenerate range & security note

When lower === upper, lodash returns that constant. For anything sensitive, skip lodash and use cryptographic randomness.

javascript
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);
Try it Yourself

📋 _.random vs Math.random

GoalMath.random()_.random(...)
Float in [0, 1)Built inNot the default; use two floats or floating: true on an interval
Integer dice 1..6Math.floor(Math.random() * 6) + 1random(1, 6)
Coin flip 0|1Math.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

Security

Predictable output

Attackers model Math.random. Never issue passwords, OTP seeds, or lottery winners that matter with _.random.

Empty arrays

Index length - 1

random(arr.length - 1) becomes random(-1) when length is 0. Guard length first.

Float surprise

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

Yes. _.random(1, 6) returns an integer from 1 through 6 inclusive—useful for dice. When lower equals upper, lodash returns that single value.
When floating is truthy (third argument), or when either bound is already a floating-point number. Otherwise, with two integer bounds, you get an integer.
Either 0 or 1 (integers)—a quick coin-flip style helper.
An integer from 0 to n inclusive (after coercion). It is not the same as Math.random(), which always returns a float in [0, 1).
No. It uses Math.random. For tokens, session IDs, or keys, use crypto.getRandomValues in browsers or crypto.randomBytes in Node.js.
Use import random from "lodash/random"; for ESM or const random = require('lodash/random') in CommonJS.

Summary

Did you know?

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

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.

6 people found this page helpful