Comparing version 1.0.3 to 1.0.4
{ | ||
"name": "random", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "Cameron Kenneth Knight <ckknight@gmail.com>" |
@@ -0,1 +1,10 @@ | ||
# 1.0.4 (2014-05-11) | ||
- `string('')` now throws an error | ||
- `hex(upper)` now returns a cached function, for efficiency. | ||
- add `int32()`, which returns an integer from `-0x80000000` to `0x7fffffff`. | ||
- performance enhancements | ||
- add benchmark suite, covers engines, integer, bool, and real. | ||
- add `date(start, end)`, which returns a Date within a uniform distribution. | ||
# 1.0.3 (2014-04-23) | ||
@@ -2,0 +11,0 @@ |
@@ -50,3 +50,3 @@ /*jshint eqnull:true*/ | ||
nativeMath: function () { | ||
return (Math.random() * 0x100000000) >>> 0; | ||
return (Math.random() * 0x100000000) | 0; | ||
}, | ||
@@ -124,3 +124,3 @@ mt19937: (function (Int32Array) { | ||
index = (index + 1) | 0; | ||
return temper(value) >>> 0; | ||
return temper(value) | 0; | ||
} | ||
@@ -159,3 +159,3 @@ next.discard = function (count) { | ||
}(typeof Int32Array === "function" ? Int32Array : Array)), | ||
browserCrypto: (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function" && typeof Uint32Array === "function") ? (function () { | ||
browserCrypto: (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function" && typeof Int32Array === "function") ? (function () { | ||
var data = null; | ||
@@ -167,3 +167,3 @@ var index = 128; | ||
if (data === null) { | ||
data = new Uint32Array(128); | ||
data = new Int32Array(128); | ||
} | ||
@@ -174,3 +174,3 @@ crypto.getRandomValues(data); | ||
return data[index++] >>> 0; | ||
return data[index++] | 0; | ||
}; | ||
@@ -196,2 +196,10 @@ }()) : null | ||
// [-0x80000000, 0x7fffffff] | ||
Random.int32 = function (engine) { | ||
return engine() | 0; | ||
}; | ||
proto.int32 = function () { | ||
return Random.int32(this.engine); | ||
}; | ||
// [0, 0xffffffff] | ||
@@ -363,3 +371,7 @@ Random.uint32 = function (engine) { | ||
} else if (range === 0xffffffff) { | ||
return add(Random.uint32, min); | ||
if (min === 0) { | ||
return Random.uint32; | ||
} else { | ||
return add(Random.int32, min + 0x80000000); | ||
} | ||
} else if (range < 0xffffffff) { | ||
@@ -457,3 +469,3 @@ return add(downscaleToRange(range), min); | ||
if (scaled % 1 === 0) { | ||
return lessThan(Random.uint32, scaled); | ||
return lessThan(Random.int32, (scaled - 0x80000000) | 0); | ||
} else { | ||
@@ -502,3 +514,3 @@ return lessThan(Random.uint53, Math.round(percentage * 0x20000000000000)); | ||
Random.pick = function (engine, array, begin, end) { | ||
var length = array.length >>> 0; | ||
var length = array.length; | ||
var start = begin == null ? 0 : convertSliceArgument(toInteger(begin), length); | ||
@@ -522,3 +534,3 @@ var finish = end === void 0 ? length : convertSliceArgument(toInteger(end), length); | ||
var clone = slice.call(array, begin, end); | ||
if (clone.length === 0) { | ||
if (!clone.length) { | ||
return returnUndefined; | ||
@@ -534,3 +546,3 @@ } | ||
var length = array.length; | ||
if (length !== 0) { | ||
if (length) { | ||
if (downTo == null) { | ||
@@ -606,4 +618,4 @@ downTo = 0; | ||
var a = engine() >>> 0; | ||
var b = engine() >>> 0; | ||
var c = engine() >>> 0; | ||
var b = engine() | 0; | ||
var c = engine() | 0; | ||
var d = engine() >>> 0; | ||
@@ -616,7 +628,7 @@ | ||
"-" + | ||
zeroPad((((b >>> 4) & 0x0fff) | 0x4000).toString(16), 4) + | ||
zeroPad((((b >> 4) & 0x0fff) | 0x4000).toString(16), 4) + | ||
"-" + | ||
zeroPad(((c & 0x3fff) | 0x8000).toString(16), 4) + | ||
"-" + | ||
zeroPad(((c >>> 4) & 0xffff).toString(16), 4) + | ||
zeroPad(((c >> 4) & 0xffff).toString(16), 4) + | ||
zeroPad(d.toString(16), 8)); | ||
@@ -638,3 +650,8 @@ }; | ||
var distribution = Random.integer(0, pool.length - 1); | ||
var length = pool.length; | ||
if (!length) { | ||
throw new Error("Expected pool not to be an empty string"); | ||
} | ||
var distribution = Random.integer(0, length - 1); | ||
return function (engine, length) { | ||
@@ -656,6 +673,11 @@ var result = ""; | ||
var LOWER_HEX_POOL = "0123456789abcdef"; | ||
var UPPER_HEX_POOL = LOWER_HEX_POOL.toUpperCase(); | ||
var lowerHex = Random.string(LOWER_HEX_POOL); | ||
var upperHex = Random.string(LOWER_HEX_POOL.toUpperCase()); | ||
return function (upper) { | ||
return Random.string(upper ? UPPER_HEX_POOL : LOWER_HEX_POOL); | ||
if (upper) { | ||
return upperHex; | ||
} else { | ||
return lowerHex; | ||
} | ||
}; | ||
@@ -667,2 +689,17 @@ }()); | ||
Random.date = function (start, end) { | ||
if (!(start instanceof Date)) { | ||
throw new TypeError("Expected start to be a Date, got " + typeof start); | ||
} else if (!(end instanceof Date)) { | ||
throw new TypeError("Expected end to be a Date, got " + typeof end); | ||
} | ||
var distribution = Random.integer(start.getTime(), end.getTime()); | ||
return function (engine) { | ||
return new Date(distribution(engine)); | ||
}; | ||
}; | ||
proto.date = function (start, end) { | ||
return Random.date(start, end)(this.engine); | ||
}; | ||
if (typeof define === "function" && define.amd) { | ||
@@ -669,0 +706,0 @@ define(function () { |
@@ -1,1 +0,1 @@ | ||
!function(n){"use strict";function t(n){if(!(this instanceof t))return new t(n);if(null==n)n=t.engines.nativeMath;else if("function"!=typeof n)throw new TypeError("Expected engine to be a function, got "+typeof n);this.engine=n}function r(n){return function(){return n}}function e(n,t){return 0===t?n:function(r){return n(r)+t}}function i(n){var t=+n;return 0>t?Math.ceil(t):Math.floor(t)}function u(n,t){return 0>n?Math.max(n+t,0):Math.min(n,t)}function o(){return void 0}var f="Random",c="function"!=typeof Math.imul||-5!==Math.imul(4294967295,5)?function(n,t){var r=n>>>16&65535,e=65535&n,i=t>>>16&65535,u=65535&t;return e*u+(r*u+e*i<<16>>>0)|0}:Math.imul,a="function"==typeof String.prototype.repeat&&"xxx"==="x".repeat(3)?function(n,t){return n.repeat(t)}:function(n,t){for(var r="";t>0;)1&t&&(r+=n),t>>=1,n+=n;return r},l=t.prototype;t.engines={nativeMath:function(){return 4294967296*Math.random()>>>0},mt19937:function(n){function r(n){for(var t=0,r=0;227>(0|t);t=t+1|0)r=2147483648&n[t]|2147483647&n[t+1|0],n[t]=n[t+397|0]^r>>>1^(1&r?2567483615:0);for(;623>(0|t);t=t+1|0)r=2147483648&n[t]|2147483647&n[t+1|0],n[t]=n[t-227|0]^r>>>1^(1&r?2567483615:0);r=2147483648&n[623]|2147483647&n[0],n[623]=n[396]^r>>>1^(1&r?2567483615:0)}function e(n){return n^=n>>>11,n^=n<<7&2636928640,n^=n<<15&4022730752,n^n>>>18}function i(n,t){for(var r=1,e=0,i=t.length,u=0|Math.max(i,624),o=0|n[0];(0|u)>0;--u)n[r]=o=(n[r]^c(o^o>>>30,1664525))+(0|t[e])+(0|e)|0,r=r+1|0,++e,(0|r)>623&&(n[0]=n[623],r=1),e>=i&&(e=0);for(u=623;(0|u)>0;--u)n[r]=o=(n[r]^c(o^o>>>30,1566083941))-r|0,r=r+1|0,(0|r)>623&&(n[0]=n[623],r=1);n[0]=2147483648}function u(){function u(){(0|f)>=624&&(r(o),f=0);var n=o[f];return f=f+1|0,e(n)>>>0}var o=new n(624),f=0;return u.discard=function(n){for(;n-f>624;)n-=624-f,r(o),f=0;return f=f+n|0,u},u.seed=function(n){var t=0;o[0]=t=0|n;for(var r=1;624>r;r=r+1|0)o[r]=t=c(t^t>>>30,1812433253)+r|0;return f=624,u},u.seedWithArray=function(n){return u.seed(19650218),i(o,n),u},u.autoSeed=function(){return u.seedWithArray(t.generateEntropyArray())},u}return u}("function"==typeof Int32Array?Int32Array:Array),browserCrypto:"undefined"!=typeof crypto&&"function"==typeof crypto.getRandomValues&&"function"==typeof Uint32Array?function(){var n=null,t=128;return function(){return t>=128&&(null===n&&(n=new Uint32Array(128)),crypto.getRandomValues(n),t=0),n[t++]>>>0}}():null},t.generateEntropyArray=function(){var n=[];n.push(0|(new Date).getTime());for(var r=t.engines.nativeMath,e=0;16>e;++e)n[e]=0|r();return n},t.uint32=function(n){return n()>>>0},l.uint32=function(){return t.uint32(this.engine)},t.uint53=function(n){var t=2097151&n(),r=n()>>>0;return 4294967296*t+r},l.uint53=function(){return t.uint53(this.engine)},t.uint53Full=function(n){for(;;){var t=0|n();if(!(2097152&t)){var r=n()>>>0;return 4294967296*(2097151&t)+r}if(2097152===(4194303&t)&&0===(0|n()))return 9007199254740992}},l.uint53Full=function(){return t.uint53Full(this.engine)},t.int53=function(n){var t=0|n(),r=n()>>>0;return 4294967296*(2097151&t)+r+(2097152&t?-9007199254740992:0)},l.int53=function(){return t.int53(this.engine)},t.int53Full=function(n){for(;;){var t=0|n();if(!(4194304&t)){var r=n()>>>0;return 4294967296*(2097151&t)+r+(2097152&t?-9007199254740992:0)}if(4194304===(8388607&t)&&0===(0|n()))return 9007199254740992}},l.int53Full=function(){return t.int53Full(this.engine)},t.integer=function(){function n(n){return 0===(n+1&n)}function i(n){return function(t){return t()&n}}function u(n){var t=n+1,r=t*Math.floor(4294967296/t);return function(n){var e=0;do e=n()>>>0;while(e>=r);return e%t}}function o(t){return n(t)?i(t):u(t)}function f(n){return 0===(0|n)}function c(n){return function(t){var r=t()&n,e=t()>>>0;return 4294967296*r+e}}function a(n){var t=n*Math.floor(9007199254740992/n);return function(r){var e=0;do{var i=2097151&r(),u=r()>>>0;e=4294967296*i+u}while(e>=t);return e%n}}function l(t){var r=t+1;if(f(r)){var e=(r/4294967296|0)-1;if(n(e))return c(e)}return a(r)}function h(n,t){return function(r){var e=0;do{var i=0|r(),u=r()>>>0;e=4294967296*(2097151&i)+u+(2097152&i?-9007199254740992:0)}while(n>e||e>t);return e}}return function(n,i){if(n=Math.floor(n),i=Math.floor(i),-9007199254740992>n||!isFinite(n))throw new RangeError("Expected min to be at least -9007199254740992");if(i>9007199254740992||!isFinite(i))throw new RangeError("Expected max to be at most 9007199254740992");var u=i-n;return 0>=u||!isFinite(u)?r(n):4294967295===u?e(t.uint32,n):4294967295>u?e(o(u),n):9007199254740991===u?e(t.uint53,n):9007199254740991>u?e(l(u),n):i-1-n===9007199254740991?e(t.uint53Full,n):-9007199254740992===n&&9007199254740992===i?t.int53Full:-9007199254740992===n&&9007199254740991===i?t.int53:-9007199254740991===n&&9007199254740992===i?e(t.int53,1):9007199254740992===i?e(h(n-1,i-1),1):h(n,i)}}(),l.integer=function(n,r){return t.integer(n,r)(this.engine)},t.realZeroToOneInclusive=function(n){return t.uint53Full(n)/9007199254740992},l.realZeroToOneInclusive=function(){return t.realZeroToOneInclusive(this.engine)},t.realZeroToOneExclusive=function(n){return t.uint53(n)/9007199254740992},l.realZeroToOneExclusive=function(){return t.realZeroToOneExclusive(this.engine)},t.real=function(){function n(n,t){return 1===t?n:0===t?function(){return 0}:function(r){return n(r)*t}}return function(r,i,u){if(!isFinite(r))throw new RangeError("Expected left to be a finite number");if(!isFinite(i))throw new RangeError("Expected right to be a finite number");return e(n(u?t.realZeroToOneInclusive:t.realZeroToOneExclusive,i-r),r)}}(),l.real=function(n,r,e){return t.real(n,r,e)(this.engine)},t.bool=function(){function n(n){return 1===(1&n())}function e(n,t){return function(r){return n(r)<t}}function i(n){if(0>=n)return r(!1);if(n>=1)return r(!0);var i=4294967296*n;return i%1===0?e(t.uint32,i):e(t.uint53,Math.round(9007199254740992*n))}return function(u,o){return null==o?null==u?n:i(u):0>=u?r(!1):u>=o?r(!0):e(t.integer(0,o-1),u)}}(),l.bool=function(n,r){return t.bool(n,r)(this.engine)},t.pick=function(n,r,e,o){var f=r.length>>>0,c=null==e?0:u(i(e),f),a=void 0===o?f:u(i(o),f);if(c>=a)return void 0;var l=t.integer(c,a-1);return r[l(n)]},l.pick=function(n,r,e){return t.pick(this.engine,n,r,e)};var h=Array.prototype.slice;t.picker=function(n,r,e){var i=h.call(n,r,e);if(0===i.length)return o;var u=t.integer(0,i.length-1);return function(n){return i[u(n)]}},t.shuffle=function(n,r,e){var i=r.length;if(0!==i){null==e&&(e=0);for(var u=i-1>>>0;u>e;--u){var o=t.integer(0,u),f=o(n);if(u!==f){var c=r[u];r[u]=r[f],r[f]=c}}}return r},l.shuffle=function(n){return t.shuffle(this.engine,n)},t.sample=function(n,r,e){if(0>e||e>r.length||!isFinite(e))throw new RangeError("Expected sampleSize to be within 0 and the length of the population");if(0===e)return[];var i=h.call(r),u=i.length;if(u===e)return t.shuffle(n,i,0);var o=u-e;return t.shuffle(n,i,o).slice(o)},l.sample=function(n,r){return t.sample(this.engine,n,r)},t.die=function(n){return t.integer(1,n)},l.die=function(n){return t.die(n)(this.engine)},t.dice=function(n,r){var e=t.die(n);return function(n){var t=[];t.length=r;for(var i=0;r>i;++i)t[i]=e(n);return t}},l.dice=function(n,r){return t.dice(n,r)(this.engine)},t.uuid4=function(){function n(n,t){return a("0",t-n.length)+n}return function(t){var r=t()>>>0,e=t()>>>0,i=t()>>>0,u=t()>>>0;return n(r.toString(16),8)+"-"+n((65535&e).toString(16),4)+"-"+n((e>>>4&4095|16384).toString(16),4)+"-"+n((16383&i|32768).toString(16),4)+"-"+n((i>>>4&65535).toString(16),4)+n(u.toString(16),8)}}(),l.uuid4=function(){return t.uuid4(this.engine)},t.string=function(){var n="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";return function(r){null==r&&(r=n);var e=t.integer(0,r.length-1);return function(n,t){for(var i="",u=0;t>u;++u){var o=e(n);i+=r.charAt(o)}return i}}}(),l.string=function(n,r){return t.string(r)(this.engine,n)},t.hex=function(){var n="0123456789abcdef",r=n.toUpperCase();return function(e){return t.string(e?r:n)}}(),l.hex=function(n,r){return t.hex(r)(this.engine,n)},"function"==typeof define&&define.amd?define(function(){return t}):"undefined"!=typeof module&&"function"==typeof require?module.exports=t:(!function(){var r=n[f];t.noConflict=function(){return n[f]=r,this}}(),n[f]=t)}(this); | ||
!function(n){"use strict";function t(n){if(!(this instanceof t))return new t(n);if(null==n)n=t.engines.nativeMath;else if("function"!=typeof n)throw new TypeError("Expected engine to be a function, got "+typeof n);this.engine=n}function r(n){return function(){return n}}function e(n,t){return 0===t?n:function(r){return n(r)+t}}function i(n){var t=+n;return 0>t?Math.ceil(t):Math.floor(t)}function u(n,t){return 0>n?Math.max(n+t,0):Math.min(n,t)}function o(){return void 0}var f="Random",c="function"!=typeof Math.imul||-5!==Math.imul(4294967295,5)?function(n,t){var r=n>>>16&65535,e=65535&n,i=t>>>16&65535,u=65535&t;return e*u+(r*u+e*i<<16>>>0)|0}:Math.imul,a="function"==typeof String.prototype.repeat&&"xxx"==="x".repeat(3)?function(n,t){return n.repeat(t)}:function(n,t){for(var r="";t>0;)1&t&&(r+=n),t>>=1,n+=n;return r},l=t.prototype;t.engines={nativeMath:function(){return 4294967296*Math.random()>>>0},mt19937:function(n){function r(n){for(var t=0,r=0;227>(0|t);t=t+1|0)r=2147483648&n[t]|2147483647&n[t+1|0],n[t]=n[t+397|0]^r>>>1^(1&r?2567483615:0);for(;623>(0|t);t=t+1|0)r=2147483648&n[t]|2147483647&n[t+1|0],n[t]=n[t-227|0]^r>>>1^(1&r?2567483615:0);r=2147483648&n[623]|2147483647&n[0],n[623]=n[396]^r>>>1^(1&r?2567483615:0)}function e(n){return n^=n>>>11,n^=n<<7&2636928640,n^=n<<15&4022730752,n^n>>>18}function i(n,t){for(var r=1,e=0,i=t.length,u=0|Math.max(i,624),o=0|n[0];(0|u)>0;--u)n[r]=o=(n[r]^c(o^o>>>30,1664525))+(0|t[e])+(0|e)|0,r=r+1|0,++e,(0|r)>623&&(n[0]=n[623],r=1),e>=i&&(e=0);for(u=623;(0|u)>0;--u)n[r]=o=(n[r]^c(o^o>>>30,1566083941))-r|0,r=r+1|0,(0|r)>623&&(n[0]=n[623],r=1);n[0]=2147483648}function u(){function u(){(0|f)>=624&&(r(o),f=0);var n=o[f];return f=f+1|0,e(n)>>>0}var o=new n(624),f=0;return u.discard=function(n){for(;n-f>624;)n-=624-f,r(o),f=0;return f=f+n|0,u},u.seed=function(n){var t=0;o[0]=t=0|n;for(var r=1;624>r;r=r+1|0)o[r]=t=c(t^t>>>30,1812433253)+r|0;return f=624,u},u.seedWithArray=function(n){return u.seed(19650218),i(o,n),u},u.autoSeed=function(){return u.seedWithArray(t.generateEntropyArray())},u}return u}("function"==typeof Int32Array?Int32Array:Array),browserCrypto:"undefined"!=typeof crypto&&"function"==typeof crypto.getRandomValues&&"function"==typeof Uint32Array?function(){var n=null,t=128;return function(){return t>=128&&(null===n&&(n=new Uint32Array(128)),crypto.getRandomValues(n),t=0),n[t++]>>>0}}():null},t.generateEntropyArray=function(){var n=[];n.push(0|(new Date).getTime());for(var r=t.engines.nativeMath,e=0;16>e;++e)n[e]=0|r();return n},t.uint32=function(n){return n()>>>0},l.uint32=function(){return t.uint32(this.engine)},t.uint53=function(n){var t=2097151&n(),r=n()>>>0;return 4294967296*t+r},l.uint53=function(){return t.uint53(this.engine)},t.uint53Full=function(n){for(;;){var t=0|n();if(!(2097152&t)){var r=n()>>>0;return 4294967296*(2097151&t)+r}if(2097152===(4194303&t)&&0===(0|n()))return 9007199254740992}},l.uint53Full=function(){return t.uint53Full(this.engine)},t.int53=function(n){var t=0|n(),r=n()>>>0;return 4294967296*(2097151&t)+r+(2097152&t?-9007199254740992:0)},l.int53=function(){return t.int53(this.engine)},t.int53Full=function(n){for(;;){var t=0|n();if(!(4194304&t)){var r=n()>>>0;return 4294967296*(2097151&t)+r+(2097152&t?-9007199254740992:0)}if(4194304===(8388607&t)&&0===(0|n()))return 9007199254740992}},l.int53Full=function(){return t.int53Full(this.engine)},t.integer=function(){function n(n){return 0===(n+1&n)}function i(n){return function(t){return t()&n}}function u(n){var t=n+1,r=t*Math.floor(4294967296/t);return function(n){var e=0;do e=n()>>>0;while(e>=r);return e%t}}function o(t){return n(t)?i(t):u(t)}function f(n){return 0===(0|n)}function c(n){return function(t){var r=t()&n,e=t()>>>0;return 4294967296*r+e}}function a(n){var t=n*Math.floor(9007199254740992/n);return function(r){var e=0;do{var i=2097151&r(),u=r()>>>0;e=4294967296*i+u}while(e>=t);return e%n}}function l(t){var r=t+1;if(f(r)){var e=(r/4294967296|0)-1;if(n(e))return c(e)}return a(r)}function h(n,t){return function(r){var e=0;do{var i=0|r(),u=r()>>>0;e=4294967296*(2097151&i)+u+(2097152&i?-9007199254740992:0)}while(n>e||e>t);return e}}return function(n,i){if(n=Math.floor(n),i=Math.floor(i),-9007199254740992>n||!isFinite(n))throw new RangeError("Expected min to be at least -9007199254740992");if(i>9007199254740992||!isFinite(i))throw new RangeError("Expected max to be at most 9007199254740992");var u=i-n;return 0>=u||!isFinite(u)?r(n):4294967295===u?e(t.uint32,n):4294967295>u?e(o(u),n):9007199254740991===u?e(t.uint53,n):9007199254740991>u?e(l(u),n):i-1-n===9007199254740991?e(t.uint53Full,n):-9007199254740992===n&&9007199254740992===i?t.int53Full:-9007199254740992===n&&9007199254740991===i?t.int53:-9007199254740991===n&&9007199254740992===i?e(t.int53,1):9007199254740992===i?e(h(n-1,i-1),1):h(n,i)}}(),l.integer=function(n,r){return t.integer(n,r)(this.engine)},t.realZeroToOneInclusive=function(n){return t.uint53Full(n)/9007199254740992},l.realZeroToOneInclusive=function(){return t.realZeroToOneInclusive(this.engine)},t.realZeroToOneExclusive=function(n){return t.uint53(n)/9007199254740992},l.realZeroToOneExclusive=function(){return t.realZeroToOneExclusive(this.engine)},t.real=function(){function n(n,t){return 1===t?n:0===t?function(){return 0}:function(r){return n(r)*t}}return function(r,i,u){if(!isFinite(r))throw new RangeError("Expected left to be a finite number");if(!isFinite(i))throw new RangeError("Expected right to be a finite number");return e(n(u?t.realZeroToOneInclusive:t.realZeroToOneExclusive,i-r),r)}}(),l.real=function(n,r,e){return t.real(n,r,e)(this.engine)},t.bool=function(){function n(n){return 1===(1&n())}function e(n,t){return function(r){return n(r)<t}}function i(n){if(0>=n)return r(!1);if(n>=1)return r(!0);var i=4294967296*n;return i%1===0?e(t.uint32,i):e(t.uint53,Math.round(9007199254740992*n))}return function(u,o){return null==o?null==u?n:i(u):0>=u?r(!1):u>=o?r(!0):e(t.integer(0,o-1),u)}}(),l.bool=function(n,r){return t.bool(n,r)(this.engine)},t.pick=function(n,r,e,o){var f=r.length>>>0,c=null==e?0:u(i(e),f),a=void 0===o?f:u(i(o),f);if(c>=a)return void 0;var l=t.integer(c,a-1);return r[l(n)]},l.pick=function(n,r,e){return t.pick(this.engine,n,r,e)};var h=Array.prototype.slice;t.picker=function(n,r,e){var i=h.call(n,r,e);if(0===i.length)return o;var u=t.integer(0,i.length-1);return function(n){return i[u(n)]}},t.shuffle=function(n,r,e){var i=r.length;if(0!==i){null==e&&(e=0);for(var u=i-1>>>0;u>e;--u){var o=t.integer(0,u),f=o(n);if(u!==f){var c=r[u];r[u]=r[f],r[f]=c}}}return r},l.shuffle=function(n){return t.shuffle(this.engine,n)},t.sample=function(n,r,e){if(0>e||e>r.length||!isFinite(e))throw new RangeError("Expected sampleSize to be within 0 and the length of the population");if(0===e)return[];var i=h.call(r),u=i.length;if(u===e)return t.shuffle(n,i,0);var o=u-e;return t.shuffle(n,i,o).slice(o)},l.sample=function(n,r){return t.sample(this.engine,n,r)},t.die=function(n){return t.integer(1,n)},l.die=function(n){return t.die(n)(this.engine)},t.dice=function(n,r){var e=t.die(n);return function(n){var t=[];t.length=r;for(var i=0;r>i;++i)t[i]=e(n);return t}},l.dice=function(n,r){return t.dice(n,r)(this.engine)},t.uuid4=function(){function n(n,t){return a("0",t-n.length)+n}return function(t){var r=t()>>>0,e=t()>>>0,i=t()>>>0,u=t()>>>0;return n(r.toString(16),8)+"-"+n((65535&e).toString(16),4)+"-"+n((e>>>4&4095|16384).toString(16),4)+"-"+n((16383&i|32768).toString(16),4)+"-"+n((i>>>4&65535).toString(16),4)+n(u.toString(16),8)}}(),l.uuid4=function(){return t.uuid4(this.engine)},t.string=function(){var n="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";return function(r){null==r&&(r=n);var e=r.length>>>0;if(0===e)throw new Error("Expected pool not to be an empty string");var i=t.integer(0,e-1);return function(n,t){for(var e="",u=0;t>u;++u){var o=i(n);e+=r.charAt(o)}return e}}}(),l.string=function(n,r){return t.string(r)(this.engine,n)},t.hex=function(){var n="0123456789abcdef",r=t.string(n),e=t.string(n.toUpperCase());return function(n){return n?e:r}}(),l.hex=function(n,r){return t.hex(r)(this.engine,n)},"function"==typeof define&&define.amd?define(function(){return t}):"undefined"!=typeof module&&"function"==typeof require?module.exports=t:(!function(){var r=n[f];t.noConflict=function(){return n[f]=r,this}}(),n[f]=t)}(this); |
{ | ||
"name": "random-js", | ||
"description": "A mathematically correct random number generator library for JavaScript.", | ||
"version": "1.0.3", | ||
"author": { | ||
"name": "Cameron Kenneth Knight", | ||
"email": "ckknight@gmail.com" | ||
}, | ||
"keywords": [ | ||
"random" | ||
], | ||
"homepage": "https://github.com/ckknight/random-js", | ||
"bugs": "https://github.com/ckknight/random-js/issues", | ||
"license": "MIT", | ||
"repository": "git://github.com/ckknight/random-js", | ||
"main": "lib/random", | ||
"readmeFilename": "README.md", | ||
"devDependencies": { | ||
"karma-script-launcher": "~0.1.0", | ||
"karma-chrome-launcher": "~0.1.2", | ||
"karma-firefox-launcher": "~0.1.3", | ||
"karma-html2js-preprocessor": "~0.1.0", | ||
"karma-jasmine": "~0.1.5", | ||
"karma-coffee-preprocessor": "~0.1.2", | ||
"requirejs": "~2.1.10", | ||
"karma-requirejs": "~0.2.1", | ||
"karma-phantomjs-launcher": "~0.1.1", | ||
"karma": "~0.10.9", | ||
"karma-coverage": "~0.1.5" | ||
}, | ||
"scripts": { | ||
"test": "jasmine-node lib/random.js spec/", | ||
"uglify": "uglifyjs lib/random.js -o lib/random.min.js -m -c" | ||
}, | ||
"testling": { | ||
"files": "spec/*.js" | ||
} | ||
} | ||
"name": "random-js", | ||
"description": "A mathematically correct random number generator library for JavaScript.", | ||
"version": "1.0.4", | ||
"author": { | ||
"name": "Cameron Kenneth Knight", | ||
"email": "ckknight@gmail.com" | ||
}, | ||
"keywords": [ | ||
"random" | ||
], | ||
"homepage": "https://github.com/ckknight/random-js", | ||
"bugs": "https://github.com/ckknight/random-js/issues", | ||
"license": "MIT", | ||
"repository": "git://github.com/ckknight/random-js", | ||
"main": "lib/random", | ||
"readmeFilename": "README.md", | ||
"devDependencies": { | ||
"benchmark": "^1.0.0", | ||
"karma": "~0.10.9", | ||
"karma-chrome-launcher": "~0.1.2", | ||
"karma-coffee-preprocessor": "~0.1.2", | ||
"karma-coverage": "~0.1.5", | ||
"karma-firefox-launcher": "~0.1.3", | ||
"karma-html2js-preprocessor": "~0.1.0", | ||
"karma-jasmine": "~0.1.5", | ||
"karma-phantomjs-launcher": "~0.1.1", | ||
"karma-requirejs": "~0.2.1", | ||
"karma-script-launcher": "~0.1.0", | ||
"microtime": "^0.5.1", | ||
"requirejs": "~2.1.10" | ||
}, | ||
"scripts": { | ||
"test": "jasmine-node lib/random.js spec/", | ||
"uglify": "uglifyjs lib/random.js -o lib/random.min.js -m -c" | ||
}, | ||
"testling": { | ||
"files": "spec/*.js" | ||
} | ||
} |
@@ -26,3 +26,3 @@ # Random.js | ||
* `nativeMath`: Utilizes [`Math.random()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random) and converts its result to an unsigned integer. This is appropriate to use if you do not care for a deterministic implementation. Based on the implementation (which is hidden to you as a developer), the period may be shorter than expected and start repeating itself. | ||
* `browserCrypto`: Utilizes [`crypto.getRandomValues(Uint32Array)`](https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues). Only supported on newer browsers, but promises cryptographically random numbers. | ||
* `browserCrypto`: Utilizes [`crypto.getRandomValues(Int32Array)`](https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues). Only supported on newer browsers, but promises cryptographically random numbers. | ||
* `mt19937()`: An implementation of the [Mersenne Twister](http://en.wikipedia.org/wiki/Mersenne_twister) algorithm. Not cryptographically secure, but its results are repeatable. Must be seeded with a single integer or an array of integers or call `.autoSeed()` to automatically seed initial data. Guaranteed to produce consistent results across all JavaScript implementations assuming the same seed. | ||
@@ -90,2 +90,3 @@ | ||
* `Random.hex(true)(engine, length)`: Produce a random string comprised of numbers or the characters `ABCDEF` of length `length`. | ||
* `Random.date(start, end)(engine)`: Produce a random `Date` within the inclusive range of [`start`, `end`]. `start` and `end` must both be `Date`s. | ||
@@ -143,2 +144,3 @@ An example of using `integer` would be as such: | ||
* `r.hex(length, true)`: Produce a random string comprised of numbers or the characters `ABCDEF` of length `length`. | ||
* `r.date(start, end)`: Produce a random `Date` within the inclusive range of [`start`, `end`]. `start` and `end` must both be `Date`s. | ||
@@ -200,2 +202,35 @@ ## Usage | ||
## Extending | ||
You can add your own methods to `Random` instances, as such: | ||
var random = new Random(); | ||
random.bark = function () { | ||
if (this.bool()) { | ||
return "arf!"; | ||
} else { | ||
return "woof!"; | ||
} | ||
}; | ||
random.bark(); //=> "arf!" or "woof!" | ||
This is the recommended approach, especially if you only use one instance of `Random`. | ||
Or you could even make your own subclass of Random: | ||
function MyRandom(engine) { | ||
return Random.call(this, engine); | ||
} | ||
MyRandom.prototype = Object.create(Random.prototype); | ||
MyRandom.prototype.constructor = MyRandom; | ||
MyRandom.prototype.mood = function () { | ||
switch (this.integer(0, 2)) { | ||
case 0: return "Happy"; | ||
case 1: return "Content"; | ||
case 2: return "Sad"; | ||
} | ||
} | ||
var random = new MyRandom(); | ||
random.mood(); //=> "Happy", "Content", or "Sad" | ||
## Testing | ||
@@ -202,0 +237,0 @@ |
@@ -51,9 +51,10 @@ (function (Random) { | ||
describe("when passed a number that only requires 32 bits of randomness", function () { | ||
it("returns false if the engine passes in a value >= than the percentage * " + 0x100000000, function () { | ||
it("returns false if int32 passes in a value >= than the percentage * " + 0x100000000, function () { | ||
var nextValue = 0; | ||
var percentage = 0.125; | ||
spyOn(Random, "int32").andReturn(Math.ceil(percentage * 0x100000000) - 0x80000000); | ||
var distribution = Random.bool(percentage); | ||
var actual = distribution(function () { | ||
return percentage * 0x100000000; | ||
return 0; | ||
}); | ||
@@ -64,9 +65,10 @@ | ||
it("returns true if the engine passes in a value < than the percentage * " + 0x100000000, function () { | ||
it("returns true if uint53 passes in a value < than the percentage * " + 0x100000000, function () { | ||
var nextValue = 0; | ||
var percentage = 0.125; | ||
spyOn(Random, "int32").andReturn(Math.floor(percentage * 0x100000000) - 0x80000001); | ||
var distribution = Random.bool(percentage); | ||
var actual = distribution(function () { | ||
return (percentage * 0x100000000) - 1; | ||
return 0; | ||
}); | ||
@@ -73,0 +75,0 @@ |
(function (Random) { | ||
describe("engines.browserCrypto", function () { | ||
if (typeof crypto === "undefined" || typeof crypto.getRandomValues !== "function" || typeof Uint32Array !== "function") { | ||
if (typeof crypto === "undefined" || typeof crypto.getRandomValues !== "function" || typeof Int32Array !== "function") { | ||
it("is null due to lack of support", function () { | ||
@@ -14,3 +14,3 @@ expect(Random.engines.browserCrypto).toBe(null); | ||
it("calls crypto.getRandomValues on a 128-length Uint32Array", function () { | ||
it("calls crypto.getRandomValues on a 128-length Int32Array", function () { | ||
spyOn(crypto, "getRandomValues").andCallThrough(); | ||
@@ -22,3 +22,3 @@ | ||
var arg = crypto.getRandomValues.mostRecentCall.args[0]; | ||
expect(arg instanceof Uint32Array).toBe(true); | ||
expect(arg instanceof Int32Array).toBe(true); | ||
expect(arg.length).toBe(128); | ||
@@ -25,0 +25,0 @@ discard(127); |
@@ -60,3 +60,16 @@ (function (Random) { | ||
function map(array, callback) { | ||
return times(array.length, function (i) { | ||
return callback(array[i]); | ||
}); | ||
} | ||
function toInt32Array(array) { | ||
return map(array, function (x) { | ||
return x | 0; | ||
}); | ||
} | ||
function test(seed, expectedValues, discard) { | ||
expectedValues = toInt32Array(expectedValues); | ||
it("passes tests for seed = " + seed, function () { | ||
@@ -74,2 +87,3 @@ var mt = Random.engines.mt19937(); | ||
function testArray(seed, expectedValues, discard) { | ||
expectedValues = toInt32Array(expectedValues); | ||
it("passes tests for seed = [" + seed.join(", ") + "]", function () { | ||
@@ -76,0 +90,0 @@ var mt = Random.engines.mt19937(); |
@@ -14,3 +14,3 @@ (function (Random) { | ||
it("returns the result of Math.random() converted to a UInt32", function () { | ||
it("returns the result of Math.random() converted to a Int32", function () { | ||
var expected = 0xdeadbeef; | ||
@@ -21,3 +21,3 @@ Math.random.andReturn(expected / 0x100000000); | ||
expect(actual).toBe(expected); | ||
expect(actual).toBe(expected | 0); | ||
}); | ||
@@ -31,5 +31,5 @@ | ||
expect(actual).toBe(expected); | ||
expect(actual).toBe(expected | 0); | ||
}); | ||
}); | ||
}(typeof module !== "undefined" ? require("../lib/random") : Random)); |
(function (Random) { | ||
describe("hex", function () { | ||
var owns = Object.prototype.hasOwnProperty; | ||
function countUnique(string) { | ||
var set = {}; | ||
var count = 0; | ||
var i = string.length; | ||
while (i--) { | ||
var c = string.charAt(i); | ||
if (!owns.call(set, c)) { | ||
set[c] = true; | ||
++count; | ||
} | ||
} | ||
return count; | ||
} | ||
[{ | ||
@@ -14,11 +29,13 @@ upper: true, | ||
describe("when upper = " + upper, function () { | ||
it("returns the result of string with pool = '" + pool + "'", function () { | ||
it("returns hex strings of the requested length", function () { | ||
var length = 1337; | ||
var dummy = function () {}; | ||
var regex = new RegExp("^[" + pool + "]{" + length + "}$"); | ||
spyOn(Random, "string").andReturn(dummy); | ||
var actual = Random.hex(upper); | ||
var hexer = Random.hex(upper); | ||
var actual = hexer(Random.engines.nativeMath, length); | ||
expect(actual).toBe(dummy); | ||
expect(Random.string).toHaveBeenCalledWith(pool); | ||
expect(actual).toMatch(regex); | ||
expect(countUnique(actual)).toBe(pool.length); | ||
}); | ||
@@ -25,0 +42,0 @@ }); |
@@ -255,2 +255,10 @@ (function (Random) { | ||
it("returns int32 if " + -0x80000000 + " and " + 0x7fffffff + " are passed in", function () { | ||
var expected = Random.int32; | ||
var actual = Random.integer(-0x80000000, 0x7fffffff); | ||
expect(actual).toBe(expected); | ||
}); | ||
it("returns uint32 if 0 and " + 0xffffffff + " are passed in", function () { | ||
@@ -339,3 +347,3 @@ var expected = Random.uint32; | ||
} else { | ||
return input[index++]; | ||
return input[index++] | 0; | ||
} | ||
@@ -342,0 +350,0 @@ }; |
@@ -267,3 +267,19 @@ (function (root) { | ||
["uint32", "uint53", "uint53Full", "int53", "int53Full", "realZeroToOneExclusive", "realZeroToOneInclusive"].forEach(function (method) { | ||
describe("date", function () { | ||
it("calls Random.date", function () { | ||
var now = new Date(); | ||
var later = new Date(now.getTime() + 86400); | ||
var dummy = new Date(now.getTime() + 12345); | ||
var spy = jasmine.createSpy().andReturn(dummy); | ||
spyOn(Random, "date").andReturn(spy); | ||
var actual = random.date(now, later); | ||
expect(Random.date).toHaveBeenCalledWith(now, later); | ||
expect(spy).toHaveBeenCalledWith(random.engine); | ||
expect(actual).toBe(dummy); | ||
}); | ||
}); | ||
["int32", "uint32", "uint53", "uint53Full", "int53", "int53Full", "realZeroToOneExclusive", "realZeroToOneInclusive"].forEach(function (method) { | ||
describe(method, function () { | ||
@@ -270,0 +286,0 @@ it("calls Random." + method, function () { |
@@ -53,3 +53,11 @@ (function (Random) { | ||
}); | ||
describe("with pool = ''", function () { | ||
it("throws an error", function () { | ||
expect(function () { | ||
Random.string(""); | ||
}).toThrow(new Error("Expected pool not to be an empty string")); | ||
}); | ||
}); | ||
}); | ||
}(typeof module !== "undefined" ? require("../lib/random") : Random)); |
@@ -6,3 +6,3 @@ (function (Random) { | ||
return function () { | ||
return input[index++]; | ||
return input[index++] | 0; | ||
}; | ||
@@ -9,0 +9,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
128264
31
2374
260
13