This Knowledge Tile documents the random function from the Lodash utility library (v2.4.1). The random function provides powerful random number generation with flexible parameter handling and floating-point support. It produces random numbers between specified ranges with intelligent parameter parsing and automatic floating-point detection.
Note: This tile focuses specifically on the _.random() function. Lodash is a comprehensive utility library containing 125+ functions across collections, objects, arrays, functions, and utilities - this documentation covers only the random number generation capability.
npm install lodash_.random() (specific utility from lodash library)Full library import:
// Note: lodash v2.4.1 predates ES6 modules
var _ = require("lodash");
// Access via _.random()Alternative CommonJS:
const _ = require("lodash");
// Access via _.random()Browser:
<script src="lodash.js"></script>
<!-- Access via _.random() -->Lodash v2.4.1 is organized into six main categories with 125+ utility functions:
forEach, map, filter, reduce, etc.)keys, values, assign, merge, etc.)first, last, compact, flatten, etc.)bind, curry, debounce, throttle, etc.)random, uniqueId, template, etc.chain, tap)The random function belongs to the Utilities category and serves as Lodash's primary random number generation utility, using Math.random() as its underlying source with enhanced parameter handling and type detection.
var _ = require("lodash");
// Generate random integer between 0 and 1
var basic = _.random();
// Generate random integer between 0 and max
var upToFive = _.random(5);
// Generate random integer between min and max
var range = _.random(10, 20);
// Generate floating-point number with boolean flag
var floating = _.random(5, true);
// Generate floating-point number between min and max
var floatRange = _.random(1.5, 5.2);
// Automatic floating-point detection
var autoFloat = _.random(1.2, 3.8);Produces a random number between min and max (inclusive) with flexible parameter handling.
/**
* Produces a random number between min and max (inclusive).
* If only one argument is provided, returns number between 0 and that number.
* If floating is truey or either min/max are floats, returns floating-point number.
*
* @param {number} [min=0] - The minimum possible value
* @param {number} [max=1] - The maximum possible value
* @param {boolean} [floating=false] - Specify returning a floating-point number
* @returns {number} Returns a random number
*/
_.random([min=0], [max=1], [floating=false]);Parameter Patterns:
_.random() - Returns random number between 0 and 1_.random(max) - Returns random number between 0 and max_.random(min, max) - Returns random number between min and max_.random(floating) - Returns floating-point number between 0 and 1 when floating is boolean_.random(max, floating) - Returns floating-point number between 0 and max when floating is boolean_.random(min, max, floating) - Returns floating-point number between min and maxUsage Examples:
// Integer examples
_.random(0, 5);
// => 0, 1, 2, 3, 4, or 5
_.random(5);
// => 0, 1, 2, 3, 4, or 5
// Floating-point examples
_.random(5, true);
// => floating-point number between 0 and 5
_.random(1.2, 5.2);
// => floating-point number between 1.2 and 5.2
_.random(0, 1, true);
// => floating-point number between 0 and 1
// Boolean parameter handling
_.random(true);
// => floating-point number between 0 and 1
_.random(10, true);
// => floating-point number between 0 and 10Advanced Features:
Math.random() as the underlying randomization sourceNumber.MAX_VALUE and boundary conditions