Produces a random number between min and max (inclusive) with flexible parameter handling and floating-point support
npx @tessl/cli install tessl/npm-lodash--random@2.4.00
# Lodash Random
1
2
## Overview
3
4
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.
5
6
**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.
7
8
## Package Information
9
10
- **Package Name**: lodash
11
- **Package Type**: npm
12
- **Language**: JavaScript
13
- **Installation**: `npm install lodash`
14
- **Function Documented**: `_.random()` (specific utility from lodash library)
15
- **Lodash Version**: 2.4.1 (2014)
16
17
## Core Imports
18
19
Full library import:
20
```javascript
21
// Note: lodash v2.4.1 predates ES6 modules
22
var _ = require("lodash");
23
// Access via _.random()
24
```
25
26
Alternative CommonJS:
27
```javascript
28
const _ = require("lodash");
29
// Access via _.random()
30
```
31
32
Browser:
33
```html
34
<script src="lodash.js"></script>
35
<!-- Access via _.random() -->
36
```
37
38
## Architecture
39
40
Lodash v2.4.1 is organized into six main categories with 125+ utility functions:
41
42
- **Collections**: Functions for arrays and objects (`forEach`, `map`, `filter`, `reduce`, etc.)
43
- **Objects**: Object manipulation utilities (`keys`, `values`, `assign`, `merge`, etc.)
44
- **Arrays**: Array-specific utilities (`first`, `last`, `compact`, `flatten`, etc.)
45
- **Functions**: Function utilities (`bind`, `curry`, `debounce`, `throttle`, etc.)
46
- **Utilities**: General utilities including `random`, `uniqueId`, `template`, etc.
47
- **Chaining**: Method chaining support (`chain`, `tap`)
48
49
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.
50
51
## Basic Usage
52
53
```javascript
54
var _ = require("lodash");
55
56
// Generate random integer between 0 and 1
57
var basic = _.random();
58
59
// Generate random integer between 0 and max
60
var upToFive = _.random(5);
61
62
// Generate random integer between min and max
63
var range = _.random(10, 20);
64
65
// Generate floating-point number with boolean flag
66
var floating = _.random(5, true);
67
68
// Generate floating-point number between min and max
69
var floatRange = _.random(1.5, 5.2);
70
71
// Automatic floating-point detection
72
var autoFloat = _.random(1.2, 3.8);
73
```
74
75
## Capabilities
76
77
### Random Number Generation
78
79
Produces a random number between min and max (inclusive) with flexible parameter handling.
80
81
```javascript { .api }
82
/**
83
* Produces a random number between min and max (inclusive).
84
* If only one argument is provided, returns number between 0 and that number.
85
* If floating is truey or either min/max are floats, returns floating-point number.
86
*
87
* @param {number} [min=0] - The minimum possible value
88
* @param {number} [max=1] - The maximum possible value
89
* @param {boolean} [floating=false] - Specify returning a floating-point number
90
* @returns {number} Returns a random number
91
*/
92
_.random([min=0], [max=1], [floating=false]);
93
```
94
95
**Parameter Patterns:**
96
97
- `_.random()` - Returns random number between 0 and 1
98
- `_.random(max)` - Returns random number between 0 and max
99
- `_.random(min, max)` - Returns random number between min and max
100
- `_.random(floating)` - Returns floating-point number between 0 and 1 when floating is boolean
101
- `_.random(max, floating)` - Returns floating-point number between 0 and max when floating is boolean
102
- `_.random(min, max, floating)` - Returns floating-point number between min and max
103
104
**Usage Examples:**
105
106
```javascript
107
// Integer examples
108
_.random(0, 5);
109
// => 0, 1, 2, 3, 4, or 5
110
111
_.random(5);
112
// => 0, 1, 2, 3, 4, or 5
113
114
// Floating-point examples
115
_.random(5, true);
116
// => floating-point number between 0 and 5
117
118
_.random(1.2, 5.2);
119
// => floating-point number between 1.2 and 5.2
120
121
_.random(0, 1, true);
122
// => floating-point number between 0 and 1
123
124
// Boolean parameter handling
125
_.random(true);
126
// => floating-point number between 0 and 1
127
128
_.random(10, true);
129
// => floating-point number between 0 and 10
130
```
131
132
**Advanced Features:**
133
134
- **Automatic floating-point detection**: When min or max contain decimal points, automatically returns floating-point numbers
135
- **Flexible parameter handling**: Supports multiple parameter patterns with intelligent type detection
136
- **Boolean parameter support**: Accepts boolean values in multiple positions to enable floating-point mode
137
- **Edge case handling**: Gracefully handles null, undefined, and string numeric values
138
- **High precision**: Uses precise floating-point calculations for accurate decimal results
139
140
## Implementation Notes
141
142
- Uses `Math.random()` as the underlying randomization source
143
- Integer generation uses floor-based calculation for whole numbers
144
- Floating-point generation uses precise decimal arithmetic
145
- Thread-safe with no internal state
146
- Supports edge cases including `Number.MAX_VALUE` and boundary conditions
147
- Handles string numbers by converting to numeric values
148
- Returns integers by default unless floating-point mode is explicitly enabled or detected