mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
Make _.random return 0 or 1 when no arguments are passed.
Former-commit-id: 7d20f5240d534f0091757f613c664b08e0d45759
This commit is contained in:
10
lodash.js
10
lodash.js
@@ -3827,13 +3827,12 @@
|
|||||||
/**
|
/**
|
||||||
* Produces a random number between `min` and `max` (inclusive). If only one
|
* Produces a random number between `min` and `max` (inclusive). If only one
|
||||||
* argument is passed, a number between `0` and the given number will be returned.
|
* argument is passed, a number between `0` and the given number will be returned.
|
||||||
* If no arguments are passed `_.random` will act as `Math.random`.
|
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
* @category Utilities
|
* @category Utilities
|
||||||
* @param {Number} min The minimum possible value.
|
* @param {Number} [min=0] The minimum possible value.
|
||||||
* @param {Number} max The maximum possible value.
|
* @param {Number} [max=1] The maximum possible value.
|
||||||
* @returns {Number} Returns a random number.
|
* @returns {Number} Returns a random number.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
@@ -3842,13 +3841,10 @@
|
|||||||
*
|
*
|
||||||
* _.random(5);
|
* _.random(5);
|
||||||
* // => also a number between 1 and 5
|
* // => also a number between 1 and 5
|
||||||
*
|
|
||||||
* _.random();
|
|
||||||
* // => an integer between 0 and less than 1
|
|
||||||
*/
|
*/
|
||||||
function random(min, max) {
|
function random(min, max) {
|
||||||
if (min == null && max == null) {
|
if (min == null && max == null) {
|
||||||
return nativeRandom();
|
max = 1;
|
||||||
}
|
}
|
||||||
min = +min || 0;
|
min = +min || 0;
|
||||||
if (max == null) {
|
if (max == null) {
|
||||||
|
|||||||
@@ -1163,9 +1163,9 @@
|
|||||||
QUnit.module('lodash.random');
|
QUnit.module('lodash.random');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
test('should work like `Math.random` if no arguments are passed', function() {
|
test('should return `0` or `1` when no arguments are passed', function() {
|
||||||
var actual = _.random();
|
var actual = _.random();
|
||||||
ok(actual >= 0 && actual < 1);
|
ok(actual === 0 || actual === 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('supports not passing a `max` argument', function() {
|
test('supports not passing a `max` argument', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user