diff --git a/dist/lodash.compat.js b/dist/lodash.compat.js index e9407ed87..32bf18716 100644 --- a/dist/lodash.compat.js +++ b/dist/lodash.compat.js @@ -969,7 +969,7 @@ * * @private * @param {*} value The value to clone. - * @param {boolean} [deep=false] A flag to indicate a deep clone. + * @param {boolean} [deep=false] Specify a deep clone. * @param {Function} [callback] The function to customize cloning values. * @param {Array} [stackA=[]] Tracks traversed source objects. * @param {Array} [stackB=[]] Associates clones with source counterparts. @@ -1955,7 +1955,7 @@ * @memberOf _ * @category Objects * @param {*} value The value to clone. - * @param {boolean} [deep=false] A flag to indicate a deep clone. + * @param {boolean} [deep=false] Specify a deep clone. * @param {Function} [callback] The function to customize cloning values. * @param {*} [thisArg] The `this` binding of `callback`. * @returns {*} Returns the cloned `value`. @@ -5938,35 +5938,56 @@ /** * Produces a random number between `min` and `max` (inclusive). If only one * argument is provided a number between `0` and the given number will be - * returned. + * returned. If `floating` is truey or either `min` or `max` are floats a + * floating-point number will be returned instead of an integer. * * @static * @memberOf _ * @category Utilities * @param {number} [min=0] The minimum possible value. * @param {number} [max=1] The maximum possible value. + * @param {boolean} [floating=false] Specify returning a floating-point number. * @returns {number} Returns a random number. * @example * * _.random(0, 5); - * // => a number between 0 and 5 + * // => an integer between 0 and 5 * * _.random(5); - * // => also a number between 0 and 5 + * // => also an integer between 0 and 5 + * + * _.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 */ - function random(min, max) { - if (min == null && max == null) { + function random(min, max, floating) { + var noMin = min == null, + noMax = max == null; + + if (floating == null) { + if (typeof min == 'boolean' && noMax) { + floating = min; + min = 1; + } + else if (!noMax && typeof max == 'boolean') { + floating = max; + noMax = true; + } + } + if (noMin && noMax) { max = 1; } min = +min || 0; - if (max == null) { + if (noMax) { max = min; min = 0; } else { max = +max || 0; } var rand = nativeRandom(); - return (min % 1 || max % 1) + return (floating || min % 1 || max % 1) ? min + nativeMin(rand * (max - min + parseFloat('1e-' + ((rand +'').length - 1))), max) : min + floor(rand * (max - min + 1)); } diff --git a/dist/lodash.compat.min.js b/dist/lodash.compat.min.js index 7b1552b55..9f632671f 100644 --- a/dist/lodash.compat.min.js +++ b/dist/lodash.compat.min.js @@ -25,7 +25,7 @@ return Ct(n,function(n){var e=Mt(++t);r[t]=r[e],r[e]=n}),r}function Bt(n,t,e){va }else if(r=t,null==r||e)return n?n[0]:v;return g(n,0,Ne(Ie(0,r),u))}function Rt(t,e,r){if(typeof r=="number"){var u=t?t.length:0;r=0>r?Ie(0,u+r):r||0}else if(r)return r=$t(t,e),t[r]===e?r:-1;return n(t,e,r)}function Ft(n,t,e){if(typeof t!="number"&&null!=t){var r=0,u=-1,o=n?n.length:0;for(t=y.createCallback(t,e,3);++u>>1,e(n[r])e?0:e);++t a number between 0 and 5 + * // => an integer between 0 and 5 * * _.random(5); - * // => also a number between 0 and 5 + * // => also an integer between 0 and 5 + * + * _.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 */ - function random(min, max) { - if (min == null && max == null) { + function random(min, max, floating) { + var noMin = min == null, + noMax = max == null; + + if (floating == null) { + if (typeof min == 'boolean' && noMax) { + floating = min; + min = 1; + } + else if (!noMax && typeof max == 'boolean') { + floating = max; + noMax = true; + } + } + if (noMin && noMax) { max = 1; } min = +min || 0; - if (max == null) { + if (noMax) { max = min; min = 0; } else { max = +max || 0; } var rand = nativeRandom(); - return (min % 1 || max % 1) + return (floating || min % 1 || max % 1) ? min + nativeMin(rand * (max - min + parseFloat('1e-' + ((rand +'').length - 1))), max) : min + floor(rand * (max - min + 1)); } diff --git a/dist/lodash.min.js b/dist/lodash.min.js index 9ad1f97ac..44a001ab5 100644 --- a/dist/lodash.min.js +++ b/dist/lodash.min.js @@ -25,7 +25,7 @@ if(l){var c=o(i);c?(u=t,i=c):l=!1}for(;++ru(i,c)&&f.push(c);return for(t=X.createCallback(t,e,3);++u>>1,e(n[r])e?0:e);++t/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:E,variable:"",imports:{_:X}};var Te=we?function(n,t){var e=f();e.value=t,we(n,"__bindData__",e),p(e)}:l,ze=xe||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&_e.call(n)==$||!1},We=Ne?function(n){return yt(n)?Ne(n):[]}:J,qe={"&":"&","<":"<",">":">",'"':""","'":"'"},Pe=ht(qe),Ke=te("("+We(Pe).join("|")+")","g"),Le=te("["+We(qe).join("")+"]","g"),Me=ot(function(n,t,e){ve.call(n,e)?n[e]++:n[e]=1 }),Ue=ot(function(n,t,e){(ve.call(n,e)?n[e]:n[e]=[]).push(t)}),Ve=ot(function(n,t,e){n[e]=t});Be&&H&&typeof ye=="function"&&(Lt=function(n){if(!gt(n))throw new re;return ye.apply(e,arguments)});var Ge=8==Se(w+"08")?Se:function(n,t){return Se(bt(n)?n.replace(I,""):n,t||0)};return X.after=function(n,t){if(!gt(t))throw new re;return function(){return 1>--n?t.apply(this,arguments):void 0}},X.assign=G,X.at=function(n){for(var t=arguments,e=-1,r=tt(t,!0,!1,1),t=t[2]&&t[2][t[1]]===n?1:r.length,u=Ht(t);++e a number between 0 and 5 + * // => an integer between 0 and 5 * * _.random(5); - * // => also a number between 0 and 5 + * // => also an integer between 0 and 5 + * + * _.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 */ function random(min, max) { if (min == null && max == null) { @@ -4210,10 +4218,7 @@ } else { max = +max || 0; } - var rand = nativeRandom(); - return (min % 1 || max % 1) - ? min + nativeMin(rand * (max - min + parseFloat('1e-' + ((rand +'').length - 1))), max) - : min + floor(rand * (max - min + 1)); + return min + floor(nativeRandom() * (max - min + 1)); } /** diff --git a/dist/lodash.underscore.min.js b/dist/lodash.underscore.min.js index 994068008..4892f1f9b 100644 --- a/dist/lodash.underscore.min.js +++ b/dist/lodash.underscore.min.js @@ -12,25 +12,25 @@ if(typeof o=="number")for(;++ee&&(e=t,u=n)});else for(;++ou&&(u=t);return u}function q(n,r){var t=-1,e=n?n.length:0;if(typeof e=="number")for(var u=Array(e);++targuments.length;r=i(r,e,4);var o=-1,f=n.length;if(typeof f=="number")for(u&&(t=n[++o]);++oarguments.length;return r=i(r,e,4),N(n,function(n,e,o){t=u?(u=!1,n):r(t,n,e,o)}),t}function $(n){var r=-1,t=n?n.length:0,e=Array(typeof t=="number"?t:0);return D(n,function(n){var t=Y(++r);e[r]=e[t],e[t]=n}),e}function I(n,r,t){var e;r=K(r,t,3),t=-1; +Xr(n,function(n,u,o){return u=e?e[--t]:--t,false===r(o[u],u,o)&&rr})}}function k(n,r,t){var e=-1,u=n?n.length:0;if(r=K(r,t,3),typeof u=="number")for(var o=Array(u);++ee&&(e=t,u=n)});else for(;++ou&&(u=t);return u}function q(n,r){var t=-1,e=n?n.length:0;if(typeof e=="number")for(var u=Array(e);++targuments.length;r=i(r,e,4);var o=-1,f=n.length;if(typeof f=="number")for(u&&(t=n[++o]);++oarguments.length;return r=i(r,e,4),N(n,function(n,e,o){t=u?(u=!1,n):r(t,n,e,o)}),t}function $(n){var r=-1,t=n?n.length:0,e=Array(typeof t=="number"?t:0);return D(n,function(n){var t=Y(++r);e[r]=e[t],e[t]=n}),e}function I(n,r,t){var e;r=K(r,t,3),t=-1; var u=n?n.length:0;if(typeof u=="number")for(;++tt(u,i)&&o.push(i)}return o}function P(n,r,t){var e=0,u=n?n.length:0;if(typeof r!="number"&&null!=r){var o=-1;for(r=K(r,t,3);++oe?Mr(0,u+e):e||0}else if(e)return e=G(r,t),r[e]===t?e:-1;return n(r,t,e)}function V(n,r,t){if(typeof r!="number"&&null!=r){var e=0,u=-1,o=n?n.length:0;for(r=K(r,t,3);++u>>1,t(n[e])/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,variable:""},Dr||(s=function(n){if(x(n)){e.prototype=n;var r=new e;e.prototype=null}return r||{}}),y(arguments)||(y=function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Ar.call(n,"callee")||!1 });var Ur=Nr||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Rr.call(n)==fr||!1},Vr=function(n){var r,t=[];if(!n||!vr[typeof n])return t;for(r in n)Ar.call(n,r)&&t.push(r);return t},Gr=qr?function(n){return x(n)?qr(n):[]}:Vr,Hr={"&":"&","<":"<",">":">",'"':""","'":"'"},Jr=b(Hr),Kr=RegExp("("+Gr(Jr).join("|")+")","g"),Lr=RegExp("["+Gr(Hr).join("")+"]","g"),Qr=function(n,r){var t;if(!n||!vr[typeof n])return n;for(t in n)if(r(n[t],t,n)===rr)break;return n},Xr=function(n,r){var t; if(!n||!vr[typeof n])return n;for(t in n)if(Ar.call(n,t)&&r(n[t],t,n)===rr)break;return n};j(/x/)&&(j=function(n){return typeof n=="function"&&"[object Function]"==Rr.call(n)});var Yr=c(function(n,r,t){Ar.call(n,t)?n[t]++:n[t]=1}),Zr=c(function(n,r,t){(Ar.call(n,t)?n[t]:n[t]=[]).push(r)}),nt=c(function(n,r,t){n[t]=r});u.after=function(n,r){if(!j(r))throw new TypeError;return function(){return 1>--n?r.apply(this,arguments):void 0}},u.bind=J,u.bindAll=function(n){for(var r=1u(i,f)){for(var a=t;--a;)if(0>u(r[a],f))continue n;i.push(f)}}return i},u.invert=b,u.invoke=function(n,r){var t=Ir.call(arguments,2),e=-1,u=typeof r=="function",o=n?n.length:0,i=Array(typeof o=="number"?o:0);return D(n,function(n){i[++e]=(u?r:n[r]).apply(n,t)}),i},u.keys=Gr,u.map=F,u.max=k,u.memoize=function(n,r){var t={};return function(){var e=r?r.apply(this,arguments):tr+arguments[0];return Ar.call(t,e)?t[e]:t[e]=n.apply(this,arguments)}},u.min=function(n,r,t){var e=1/0,u=e,o=-1,i=n?n.length:0; +n:for(;++eu(i,f)){for(var a=t;--a;)if(0>u(r[a],f))continue n;i.push(f)}}return i},u.invert=b,u.invoke=function(n,r){var t=Ir.call(arguments,2),e=-1,u=typeof r=="function",o=n?n.length:0,i=Array(typeof o=="number"?o:0);return D(n,function(n){i[++e]=(u?r:n[r]).apply(n,t)}),i},u.keys=Gr,u.map=k,u.max=F,u.memoize=function(n,r){var t={};return function(){var e=r?r.apply(this,arguments):tr+arguments[0];return Ar.call(t,e)?t[e]:t[e]=n.apply(this,arguments)}},u.min=function(n,r,t){var e=1/0,u=e,o=-1,i=n?n.length:0; if(r||typeof i!="number")r=K(r,t,3),D(n,function(n,t,o){t=r(n,t,o),tr(t,u)&&(e[u]=n)}),e},u.once=function(n){var r,t;if(!j(n))throw new TypeError;return function(){return r?t:(r=!0,t=n.apply(this,arguments),n=null,t)}},u.pairs=function(n){for(var r=-1,t=Gr(n),e=t.length,u=Array(e);++rr?0:r);++nr?0:r);++nt?Mr(0,e+t):Wr(t,e-1))+1);e--;)if(n[e]===r)return e;return-1},u.mixin=X,u.noConflict=function(){return yr._=jr,this},u.random=Y,u.reduce=M,u.reduceRight=W,u.result=function(n,r){if(n){var t=n[r];return j(t)?n[r]():t}},u.size=function(n){var r=n?n.length:0; return typeof r=="number"?r:Gr(n).length},u.some=I,u.sortedIndex=G,u.template=function(n,r,e){var o=u,i=o.templateSettings;n||(n=""),e=_({},e,i);var f=0,a="__p+='",i=e.variable;n.replace(RegExp((e.escape||er).source+"|"+(e.interpolate||er).source+"|"+(e.evaluate||er).source+"|$","g"),function(r,e,u,o,i){return a+=n.slice(f,i).replace(or,t),e&&(a+="'+_.escape("+e+")+'"),o&&(a+="';"+o+";__p+='"),u&&(a+="'+((__t=("+u+"))==null?'':__t)+'"),f=i+r.length,r}),a+="';\n",i||(i="obj",a="with("+i+"||{}){"+a+"}"),a="function("+i+"){var __t,__p='',__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+a+"return __p}"; try{var l=Function("_","return "+a)(o)}catch(c){throw c.source=a,c}return r?l(r):(l.source=a,l)},u.unescape=function(n){return null==n?"":(n+"").replace(Kr,g)},u.uniqueId=function(n){var r=++nr+"";return n?n+r:r},u.all=R,u.any=I,u.detect=B,u.findWhere=function(n,r){return z(n,r,!0)},u.foldl=M,u.foldr=W,u.include=O,u.inject=M,u.first=P,u.last=function(n,r,t){var e=0,u=n?n.length:0;if(typeof r!="number"&&null!=r){var o=u;for(r=K(r,t,3);o--&&r(n[o],o,n);)e++}else if(e=r,null==e||t)return n?n[u-1]:Z; diff --git a/doc/README.md b/doc/README.md index 9b0a1ce9b..b9274ef46 100644 --- a/doc/README.md +++ b/doc/README.md @@ -174,7 +174,7 @@ * [`_.mixin`](#_mixinobject-object) * [`_.noConflict`](#_noconflict) * [`_.parseInt`](#_parseintvalue--radix) -* [`_.random`](#_randommin0--max1) +* [`_.random`](#_randommin0--max1--floatingfalse) * [`_.result`](#_resultobject-property) * [`_.runInContext`](#_runincontextcontextroot) * [`_.template`](#_templatetext-data--options--optionsescape--optionsevaluate--optionsimports--optionsinterpolate--sourceurl--variable) @@ -1064,7 +1064,7 @@ _.isArray(squares.value()); ### `_.chain(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6312 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6333 "View in source") [Ⓣ][1] Creates a `lodash` object that wraps the given value. @@ -1097,7 +1097,7 @@ var youngest = _.chain(stooges) ### `_.tap(value, interceptor)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6340 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6361 "View in source") [Ⓣ][1] Invokes `interceptor` with the `value` as the first argument and then returns `value`. The purpose of this method is to "tap into" a method chain in order to perform operations on intermediate results within the chain. @@ -1127,7 +1127,7 @@ _([1, 2, 3, 4]) ### `_.prototype.chain()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6360 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6381 "View in source") [Ⓣ][1] Enables method chaining on the wrapper object. @@ -1151,7 +1151,7 @@ var sum = _([1, 2, 3]) ### `_.prototype.toString()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6377 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6398 "View in source") [Ⓣ][1] Produces the `toString` result of the wrapped value. @@ -1172,7 +1172,7 @@ _([1, 2, 3]).toString(); ### `_.prototype.valueOf()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6394 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6415 "View in source") [Ⓣ][1] Extracts the wrapped value. @@ -2732,7 +2732,7 @@ Creates a clone of `value`. If `deep` is `true` nested objects will also be clon #### Arguments 1. `value` *(\*)*: The value to clone. -2. `[deep=false]` *(boolean)*: A flag to indicate a deep clone. +2. `[deep=false]` *(boolean)*: Specify a deep clone. 3. `[callback]` *(Function)*: The function to customize cloning values. 4. `[thisArg]` *(\*)*: The `this` binding of `callback`. @@ -3953,14 +3953,15 @@ _.parseInt('08'); -### `_.random([min=0], [max=1])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5976 "View in source") [Ⓣ][1] +### `_.random([min=0], [max=1], [floating=false])` +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5984 "View in source") [Ⓣ][1] -Produces a random number between `min` and `max` *(inclusive)*. If only one argument is provided a number between `0` and the given number will be returned. +Produces a random number between `min` and `max` *(inclusive)*. If only one argument is provided a number between `0` and the given number will be returned. If `floating` is truey or either `min` or `max` are floats a floating-point number will be returned instead of an integer. #### Arguments 1. `[min=0]` *(number)*: The minimum possible value. 2. `[max=1]` *(number)*: The maximum possible value. +3. `[floating=false]` *(boolean)*: Specify returning a floating-point number. #### Returns *(number)*: Returns a random number. @@ -3968,10 +3969,16 @@ Produces a random number between `min` and `max` *(inclusive)*. If only one argu #### Example ```js _.random(0, 5); -// => a number between 0 and 5 +// => an integer between 0 and 5 _.random(5); -// => also a number between 0 and 5 +// => also an integer between 0 and 5 + +_.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 ``` * * * @@ -3982,7 +3989,7 @@ _.random(5); ### `_.result(object, property)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6020 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6041 "View in source") [Ⓣ][1] Resolves the value of `property` on `object`. If `property` is a function it will be invoked with the `this` binding of `object` and its result returned, else the property value is returned. If `object` is falsey then `undefined` is returned. @@ -4035,7 +4042,7 @@ Create a new `lodash` function using the given context object. ### `_.template(text, data, [options], [options.escape], [options.evaluate], [options.imports], [options.interpolate], [sourceURL], [variable])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6113 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6134 "View in source") [Ⓣ][1] A micro-templating method that handles arbitrary delimiters, preserves whitespace, and correctly escapes quotes within interpolated code. @@ -4129,7 +4136,7 @@ fs.writeFileSync(path.join(cwd, 'jst.js'), '\ ### `_.times(n, callback, [thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6236 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6257 "View in source") [Ⓣ][1] Executes the callback `n` times, returning an array of the results of each callback execution. The callback is bound to `thisArg` and invoked with one argument; *(index)*. @@ -4161,7 +4168,7 @@ _.times(3, function(n) { this.cast(n); }, mage); ### `_.unescape(string)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6263 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6284 "View in source") [Ⓣ][1] The inverse of `_.escape` this method converts the HTML entities `&`, `<`, `>`, `"`, and `'` in `string` to their corresponding characters. @@ -4185,7 +4192,7 @@ _.unescape('Moe, Larry & Curly'); ### `_.uniqueId([prefix])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6283 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6304 "View in source") [Ⓣ][1] Generates a unique ID. If `prefix` is provided the ID will be appended to it. @@ -4238,7 +4245,7 @@ A reference to the `lodash` function. ### `_.VERSION` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6592 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6613 "View in source") [Ⓣ][1] *(string)*: The semantic version number. diff --git a/lodash.js b/lodash.js index a4cf4864e..5a9ca1279 100644 --- a/lodash.js +++ b/lodash.js @@ -987,7 +987,7 @@ * * @private * @param {*} value The value to clone. - * @param {boolean} [deep=false] A flag to indicate a deep clone. + * @param {boolean} [deep=false] Specify a deep clone. * @param {Function} [callback] The function to customize cloning values. * @param {Array} [stackA=[]] Tracks traversed source objects. * @param {Array} [stackB=[]] Associates clones with source counterparts. @@ -1974,7 +1974,7 @@ * @memberOf _ * @category Objects * @param {*} value The value to clone. - * @param {boolean} [deep=false] A flag to indicate a deep clone. + * @param {boolean} [deep=false] Specify a deep clone. * @param {Function} [callback] The function to customize cloning values. * @param {*} [thisArg] The `this` binding of `callback`. * @returns {*} Returns the cloned `value`. @@ -5957,35 +5957,56 @@ /** * Produces a random number between `min` and `max` (inclusive). If only one * argument is provided a number between `0` and the given number will be - * returned. + * returned. If `floating` is truey or either `min` or `max` are floats a + * floating-point number will be returned instead of an integer. * * @static * @memberOf _ * @category Utilities * @param {number} [min=0] The minimum possible value. * @param {number} [max=1] The maximum possible value. + * @param {boolean} [floating=false] Specify returning a floating-point number. * @returns {number} Returns a random number. * @example * * _.random(0, 5); - * // => a number between 0 and 5 + * // => an integer between 0 and 5 * * _.random(5); - * // => also a number between 0 and 5 + * // => also an integer between 0 and 5 + * + * _.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 */ - function random(min, max) { - if (min == null && max == null) { + function random(min, max, floating) { + var noMin = min == null, + noMax = max == null; + + if (floating == null) { + if (typeof min == 'boolean' && noMax) { + floating = min; + min = 1; + } + else if (!noMax && typeof max == 'boolean') { + floating = max; + noMax = true; + } + } + if (noMin && noMax) { max = 1; } min = +min || 0; - if (max == null) { + if (noMax) { max = min; min = 0; } else { max = +max || 0; } var rand = nativeRandom(); - return (min % 1 || max % 1) + return (floating || min % 1 || max % 1) ? min + nativeMin(rand * (max - min + parseFloat('1e-' + ((rand +'').length - 1))), max) : min + floor(rand * (max - min + 1)); } diff --git a/test/test.js b/test/test.js index 39b04efa8..81a7f65c9 100644 --- a/test/test.js +++ b/test/test.js @@ -3065,6 +3065,17 @@ ok(actual % 1); ok(actual >= min && actual <= max); }); + + test('supports passing a `floating` argument', function() { + var actual = _.random(true); + ok(actual % 1 && actual >= 0 && actual <= 1); + + actual = _.random(2, true); + ok(actual % 1 && actual >= 0 && actual <= 2); + + actual = _.random(2, 4, true); + ok(actual % 1 && actual >= 2 && actual <= 4); + }); }()); /*--------------------------------------------------------------------------*/