Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "random", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "Cameron Kenneth Knight <ckknight@gmail.com>" |
@@ -0,1 +1,5 @@ | ||
# 1.0.2 (2014-03-20) | ||
- Even if `shuffle` receives an empty array, it should return that same array. | ||
# 1.0.1 (2014-03-20) | ||
@@ -2,0 +6,0 @@ |
@@ -29,3 +29,3 @@ // Karma configuration | ||
preprocessors: { | ||
'random.js': 'coverage' | ||
'lib/random.js': 'coverage' | ||
}, | ||
@@ -32,0 +32,0 @@ |
@@ -495,16 +495,16 @@ /*jshint eqnull:true*/ | ||
Random.shuffle = function (engine, array, downTo) { | ||
if (array.length === 0) { | ||
return []; | ||
} | ||
if (downTo == null) { | ||
downTo = 0; | ||
} | ||
for (var i = (array.length - 1) >>> 0; i > downTo; --i) { | ||
var distribution = Random.integer(0, i); | ||
var j = distribution(engine); | ||
if (i !== j) { | ||
var tmp = array[i]; | ||
array[i] = array[j]; | ||
array[j] = tmp; | ||
var length = array.length; | ||
if (length !== 0) { | ||
if (downTo == null) { | ||
downTo = 0; | ||
} | ||
for (var i = (length - 1) >>> 0; i > downTo; --i) { | ||
var distribution = Random.integer(0, i); | ||
var j = distribution(engine); | ||
if (i !== j) { | ||
var tmp = array[i]; | ||
array[i] = array[j]; | ||
array[j] = tmp; | ||
} | ||
} | ||
} | ||
@@ -511,0 +511,0 @@ return array; |
@@ -1,1 +0,1 @@ | ||
!function(n){"use strict";function t(n){if(!(this instanceof t))throw new TypeError("Expected to be called with new");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}}var i="Random",u="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,o="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},f=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,o=0|Math.max(i,624),f=0|n[0];(0|o)>0;--o)n[r]=f=(n[r]^u(f^f>>>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(o=623;(0|o)>0;--o)n[r]=f=(n[r]^u(f^f>>>30,1566083941))-r|0,r=r+1|0,(0|r)>623&&(n[0]=n[623],r=1);n[0]=2147483648}function o(){function o(){(0|c)>=624&&(r(f),c=0);var n=f[c];return c=c+1|0,e(n)>>>0}var f=new n(624),c=0;return o.discard=function(n){for(;n-c>624;)n-=624-c,r(f),c=0;return c=c+n|0,o},o.seed=function(n){var t=0;f[0]=t=0|n;for(var r=1;624>r;r=r+1|0)f[r]=t=u(t^t>>>30,1812433253)+r|0;return c=624,o},o.seedWithArray=function(n){return o.seed(19650218),i(f,n),o},o.autoSeed=function(){return o.seedWithArray(t.generateEntropyArray())},o}return o}("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},f.uint32=function(){return t.uint32(this.engine)},t.uint53=function(n){var t=2097151&n(),r=n()>>>0;return 4294967296*t+r},f.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}},f.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)},f.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}},f.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)}}(),f.integer=function(n,r){return t.integer(n,r)(this.engine)},t.realZeroToOneInclusive=function(n){return t.uint53Full(n)/9007199254740992},f.realZeroToOneInclusive=function(){return t.realZeroToOneInclusive(this.engine)},t.realZeroToOneExclusive=function(n){return t.uint53(n)/9007199254740992},f.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)}}(),f.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)}}(),f.bool=function(n,r){return t.bool(n,r)(this.engine)},t.pick=function(n,r){var e=t.integer(0,r.length-1>>>0);return r[e(n)]},f.pick=function(n){return t.pick(this.engine,n)};var c=Array.prototype.slice;t.picker=function(n){var r=c.call(n),e=t.integer(0,r.length-1);return function(n){return r[e(n)]}},t.shuffle=function(n,r,e){if(0===r.length)return[];null==e&&(e=0);for(var i=r.length-1>>>0;i>e;--i){var u=t.integer(0,i),o=u(n);if(i!==o){var f=r[i];r[i]=r[o],r[o]=f}}return r},f.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=c.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)},f.sample=function(n,r){return t.sample(this.engine,n,r)},t.die=function(n){return t.integer(1,n)},f.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}},f.dice=function(n,r){return t.dice(n,r)(this.engine)},t.uuid4=function(){function n(n,t){return o("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)}}(),f.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}}}(),f.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)}}(),f.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[i];t.noConflict=function(){return n[i]=r,this}}(),n[i]=t)}(this); | ||
!function(n){"use strict";function t(n){if(!(this instanceof t))throw new TypeError("Expected to be called with new");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}}var i="Random",u="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,o="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},f=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,o=0|Math.max(i,624),f=0|n[0];(0|o)>0;--o)n[r]=f=(n[r]^u(f^f>>>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(o=623;(0|o)>0;--o)n[r]=f=(n[r]^u(f^f>>>30,1566083941))-r|0,r=r+1|0,(0|r)>623&&(n[0]=n[623],r=1);n[0]=2147483648}function o(){function o(){(0|c)>=624&&(r(f),c=0);var n=f[c];return c=c+1|0,e(n)>>>0}var f=new n(624),c=0;return o.discard=function(n){for(;n-c>624;)n-=624-c,r(f),c=0;return c=c+n|0,o},o.seed=function(n){var t=0;f[0]=t=0|n;for(var r=1;624>r;r=r+1|0)f[r]=t=u(t^t>>>30,1812433253)+r|0;return c=624,o},o.seedWithArray=function(n){return o.seed(19650218),i(f,n),o},o.autoSeed=function(){return o.seedWithArray(t.generateEntropyArray())},o}return o}("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},f.uint32=function(){return t.uint32(this.engine)},t.uint53=function(n){var t=2097151&n(),r=n()>>>0;return 4294967296*t+r},f.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}},f.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)},f.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}},f.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)}}(),f.integer=function(n,r){return t.integer(n,r)(this.engine)},t.realZeroToOneInclusive=function(n){return t.uint53Full(n)/9007199254740992},f.realZeroToOneInclusive=function(){return t.realZeroToOneInclusive(this.engine)},t.realZeroToOneExclusive=function(n){return t.uint53(n)/9007199254740992},f.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)}}(),f.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)}}(),f.bool=function(n,r){return t.bool(n,r)(this.engine)},t.pick=function(n,r){var e=t.integer(0,r.length-1>>>0);return r[e(n)]},f.pick=function(n){return t.pick(this.engine,n)};var c=Array.prototype.slice;t.picker=function(n){var r=c.call(n),e=t.integer(0,r.length-1);return function(n){return r[e(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},f.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=c.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)},f.sample=function(n,r){return t.sample(this.engine,n,r)},t.die=function(n){return t.integer(1,n)},f.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}},f.dice=function(n,r){return t.dice(n,r)(this.engine)},t.uuid4=function(){function n(n,t){return o("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)}}(),f.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}}}(),f.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)}}(),f.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[i];t.noConflict=function(){return n[i]=r,this}}(),n[i]=t)}(this); |
{ | ||
"name": "random-js", | ||
"description": "A mathematically correct random number generator library for JavaScript.", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Cameron Kenneth Knight", |
@@ -174,3 +174,3 @@ # Random.js | ||
<script> | ||
var Random = require("random-js"); | ||
// Random is now available as a global (on the window object) | ||
var random = new Random(Random.engines.mt19937().autoSeed()); | ||
@@ -177,0 +177,0 @@ alert("Random value from 1 to 100: " + random.integer(1, 100)); |
@@ -18,5 +18,6 @@ (function (Random) { | ||
var actual = Random.shuffle(engine, ["a", "b", "c", "d", "e"]); | ||
var actual = Random.shuffle(engine, array); | ||
expect(actual).toEqual(expected); | ||
expect(actual).toBe(array); | ||
}); | ||
@@ -41,5 +42,6 @@ | ||
var actual = Random.shuffle(engine, ["a", "b", "c", "d", "e"], downTo); | ||
var actual = Random.shuffle(engine, array, downTo); | ||
expect(actual).toEqual(expected); | ||
expect(actual).toBe(array); | ||
}); | ||
@@ -49,3 +51,3 @@ }); | ||
describe("with an empty array", function () { | ||
it("returns a new empty array", function () { | ||
it("returns the same empty array", function () { | ||
var engine = function () {}; | ||
@@ -57,3 +59,3 @@ var array = []; | ||
expect(actual).toEqual([]); | ||
expect(actual).not.toBe(array); | ||
expect(actual).toBe(array); | ||
}); | ||
@@ -60,0 +62,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
108488
1889