Number methods clamp, bound, and generate numeric values.
_.clamp(number, [lower], upper)
Available since v4.0.0.
Clamps number within the inclusive lower and upper bounds.
| Parameter | Type | Description |
|---|---|---|
number | number | The number to clamp. |
[lower] | number | The lower bound. |
upper | number | The upper bound. |
Returns number — Returns the clamped number.
_.clamp(-10, -5, 5);
// => -5
_.clamp(10, -5, 5);
// => 5_.inRange(number, [start=0], end)
Available since v3.3.0.
Checks if n is between start and up to, but not including, end. If end is not specified, it's set to start with start then set to 0. If start is greater than end the params are swapped to support negative ranges.
| Parameter | Type | Description |
|---|---|---|
number | number | The number to check. |
[start=0] | number | The start of the range. |
end | number | The end of the range. |
Returns boolean — Returns true if number is in the range, else false.
_.inRange(3, 2, 4);
// => true
_.inRange(4, 8);
// => true
_.inRange(4, 2);
// => false
_.inRange(2, 2);
// => false
_.inRange(1.2, 2);
// => true
_.inRange(5.2, 4);
// => false
_.inRange(-3, -2, -6);
// => true_.random([lower=0], [upper=1], [floating])
Available since v0.7.0.
Produces a random number between the inclusive lower and upper bounds. If only one argument is provided a number between 0 and the given number is returned. If floating is true, or either lower or upper are floats, a floating-point number is returned instead of an integer.
Note: JavaScript follows the IEEE-754 standard for resolving floating-point values which can produce unexpected results.
Note: If lower is greater than upper, the values are swapped.
| Parameter | Type | Description |
|---|---|---|
[lower=0] | number | The lower bound. |
[upper=1] | number | The upper bound. |
[floating] | boolean | Specify returning a floating-point number. |
Returns number — Returns the random number.
_.random(0, 5);
// => an integer between 0 and 5
// when lower is greater than upper the values are swapped
_.random(5, 0);
// => an integer between 0 and 5
_.random(5);
// => also an integer between 0 and 5
_.random(-5);
// => an integer between -5 and 0
_.random(5, true);
// => a floating-point number between 0 and 5
_.random(1.2, 5.2);
// => a floating-point number between 1.2 and 5.2