or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash-random

Produces a random number between min and max (inclusive) with flexible parameter handling and floating-point support

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.random@2.4.x

To install, run

npx @tessl/cli install tessl/npm-lodash-random@2.4.0

index.mddocs/

Lodash Random

Overview

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.

Package Information

  • Package Name: lodash
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash
  • Function Documented: _.random() (specific utility from lodash library)
  • Lodash Version: 2.4.1 (2014)

Core Imports

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

Architecture

Lodash v2.4.1 is organized into six main categories with 125+ utility functions:

  • Collections: Functions for arrays and objects (forEach, map, filter, reduce, etc.)
  • Objects: Object manipulation utilities (keys, values, assign, merge, etc.)
  • Arrays: Array-specific utilities (first, last, compact, flatten, etc.)
  • Functions: Function utilities (bind, curry, debounce, throttle, etc.)
  • Utilities: General utilities including random, uniqueId, template, etc.
  • Chaining: Method chaining support (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.

Basic Usage

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

Capabilities

Random Number Generation

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 max

Usage 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 10

Advanced Features:

  • Automatic floating-point detection: When min or max contain decimal points, automatically returns floating-point numbers
  • Flexible parameter handling: Supports multiple parameter patterns with intelligent type detection
  • Boolean parameter support: Accepts boolean values in multiple positions to enable floating-point mode
  • Edge case handling: Gracefully handles null, undefined, and string numeric values
  • High precision: Uses precise floating-point calculations for accurate decimal results

Implementation Notes

  • Uses Math.random() as the underlying randomization source
  • Integer generation uses floor-based calculation for whole numbers
  • Floating-point generation uses precise decimal arithmetic
  • Thread-safe with no internal state
  • Supports edge cases including Number.MAX_VALUE and boundary conditions
  • Handles string numbers by converting to numeric values
  • Returns integers by default unless floating-point mode is explicitly enabled or detected