mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 23:37:49 +00:00
Update _.random docs and add unit tests.
Former-commit-id: d4a033bf83ad5fce7bb3b09467305730a89adc01
This commit is contained in:
@@ -2852,10 +2852,10 @@ Produces a random number between `min` and `max` *(inclusive)*. If only one argu
|
||||
#### Example
|
||||
```js
|
||||
_.random(0, 5);
|
||||
// => a number between 1 and 5
|
||||
// => a number between 0 and 5
|
||||
|
||||
_.random(5);
|
||||
// => also a number between 1 and 5
|
||||
// => also a number between 0 and 5
|
||||
```
|
||||
|
||||
* * *
|
||||
|
||||
@@ -3889,10 +3889,10 @@
|
||||
* @example
|
||||
*
|
||||
* _.random(0, 5);
|
||||
* // => a number between 1 and 5
|
||||
* // => a number between 0 and 5
|
||||
*
|
||||
* _.random(5);
|
||||
* // => also a number between 1 and 5
|
||||
* // => also a number between 0 and 5
|
||||
*/
|
||||
function random(min, max) {
|
||||
if (min == null && max == null) {
|
||||
|
||||
18
test/test.js
18
test/test.js
@@ -1396,6 +1396,24 @@
|
||||
}
|
||||
notEqual(actual, 5);
|
||||
});
|
||||
|
||||
test('supports large integer values', function() {
|
||||
var array = Array(1000),
|
||||
min = Math.pow(2, 31),
|
||||
max = Math.pow(2, 62);
|
||||
|
||||
ok(_.every(array, function() {
|
||||
return _.random(min, max) >= min;
|
||||
}));
|
||||
|
||||
ok(_.some(array, function() {
|
||||
return _.random(Number.MAX_VALUE) > 0;
|
||||
}));
|
||||
});
|
||||
|
||||
test('should coerce arguments to numbers', function() {
|
||||
strictEqual(_.random('1', '1'), 1);
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
Reference in New Issue
Block a user