Make _.random return 0 or 1 when no arguments are passed.

Former-commit-id: 7d20f5240d534f0091757f613c664b08e0d45759
This commit is contained in:
John-David Dalton
2012-09-18 20:29:36 -07:00
parent f85287a444
commit 6499643769
2 changed files with 5 additions and 9 deletions

View File

@@ -3827,13 +3827,12 @@
/**
* 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.
* If no arguments are passed `_.random` will act as `Math.random`.
*
* @static
* @memberOf _
* @category Utilities
* @param {Number} min The minimum possible value.
* @param {Number} max The maximum possible value.
* @param {Number} [min=0] The minimum possible value.
* @param {Number} [max=1] The maximum possible value.
* @returns {Number} Returns a random number.
* @example
*
@@ -3842,13 +3841,10 @@
*
* _.random(5);
* // => also a number between 1 and 5
*
* _.random();
* // => an integer between 0 and less than 1
*/
function random(min, max) {
if (min == null && max == null) {
return nativeRandom();
max = 1;
}
min = +min || 0;
if (max == null) {

View File

@@ -1163,9 +1163,9 @@
QUnit.module('lodash.random');
(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();
ok(actual >= 0 && actual < 1);
ok(actual === 0 || actual === 1);
});
test('supports not passing a `max` argument', function() {