seedrandom
Advanced tools
Comparing version 2.3.10 to 2.3.11
{ | ||
"name": "seedrandom", | ||
"version": "2.3.6", | ||
"version": "2.3.11", | ||
"description": "Seeded random number generator for Javascript", | ||
@@ -5,0 +5,0 @@ "repository": "davidbau/seedrandom", |
{ | ||
"name": "seedrandom", | ||
"version": "2.3.10", | ||
"version": "2.3.11", | ||
"description": "Seeded random number generator for Javascript.", | ||
@@ -42,4 +42,5 @@ "main": "seedrandom.js", | ||
"phantomjs": "latest", | ||
"proxyquire": "git://github.com/davidbau/proxyquire.git#nullstub" | ||
"proxyquire": "git://github.com/davidbau/proxyquire.git#nullstub", | ||
"requirejs": "latest" | ||
} | ||
} |
@@ -9,5 +9,5 @@ seedrandom.js | ||
version 2.3.10<br> | ||
version 2.3.11<br> | ||
Author: David Bau<br> | ||
Date: 2014 Sep 20 | ||
Date: 2014 Dec 11 | ||
@@ -21,3 +21,3 @@ Can be used as a plain script, a node.js module or an AMD module. | ||
<pre> | ||
<script src=//cdnjs.cloudflare.com/ajax/libs/seedrandom/2.3.10/seedrandom.min.js> | ||
<script src=//cdnjs.cloudflare.com/ajax/libs/seedrandom/2.3.11/seedrandom.min.js> | ||
</script> | ||
@@ -96,3 +96,3 @@ </pre> | ||
<pre> | ||
<script src=//cdnjs.cloudflare.com/ajax/libs/seedrandom/2.3.10/seedrandom.min.js> | ||
<script src=//cdnjs.cloudflare.com/ajax/libs/seedrandom/2.3.11/seedrandom.min.js> | ||
</script> | ||
@@ -154,2 +154,20 @@ <!-- Seeds using urandom bits from a server. --> | ||
Saving and Restoring PRNG state | ||
------------------------------- | ||
<pre> | ||
var seedrandom = Math.seedrandom; | ||
var saveable = seedrandom("secret-seed", {state: true}); | ||
for (var j = 0; j < 1e5; ++j) saveable(); | ||
var saved = saveable.state(); | ||
var replica = seedrandom("", {state: saved}); | ||
assert(replica() == saveable()); | ||
</pre> | ||
In normal use the prng is opaque and its internal state cannot be accessed. | ||
However, if the "state" option is specified, the prng gets a state() method | ||
that returns a plain object the can be used to reconstruct a prng later in | ||
the same state (by passing that saved object back as the state option). | ||
Version notes | ||
@@ -168,2 +186,3 @@ ------------- | ||
* Version 2.3.10 adds support for node.js crypto (contributed by ctd1500). | ||
* Version 2.3.11 adds an option to load and save internal state. | ||
@@ -170,0 +189,0 @@ The standard ARC4 key scheduler cycles short keys, which means that |
@@ -8,5 +8,5 @@ /** | ||
version 2.3.10 | ||
version 2.3.11 | ||
Author: David Bau | ||
Date: 2014 Sep 20 | ||
Date: 2014 Dec 11 | ||
@@ -18,3 +18,3 @@ Can be used as a plain script, a node.js module or an AMD module. | ||
<script src=//cdnjs.cloudflare.com/ajax/libs/seedrandom/2.3.10/seedrandom.min.js> | ||
<script src=//cdnjs.cloudflare.com/ajax/libs/seedrandom/2.3.11/seedrandom.min.js> | ||
</script> | ||
@@ -81,3 +81,3 @@ | ||
<script src=//cdnjs.cloudflare.com/ajax/libs/seedrandom/2.3.10/seedrandom.min.js> | ||
<script src=//cdnjs.cloudflare.com/ajax/libs/seedrandom/2.3.11/seedrandom.min.js> | ||
</script> | ||
@@ -135,2 +135,18 @@ | ||
Saving and Restoring PRNG state | ||
------------------------------- | ||
var seedrandom = Math.seedrandom; | ||
var saveable = seedrandom("secret-seed", {state: true}); | ||
for (var j = 0; j < 1e5; ++j) saveable(); | ||
var saved = saveable.state(); | ||
var replica = seedrandom("", {state: saved}); | ||
assert(replica() == saveable()); | ||
In normal use the prng is opaque and its internal state cannot be accessed. | ||
However, if the "state" option is specified, the prng gets a state() method | ||
that returns a plain object the can be used to reconstruct a prng later in | ||
the same state (by passing that saved object back as the state option). | ||
Version notes | ||
@@ -148,2 +164,3 @@ ------------- | ||
* Version 2.3.10 adds support for node.js crypto (contributed by ctd1500). | ||
* Version 2.3.11 adds an option to load and save internal state. | ||
@@ -245,7 +262,16 @@ The standard ARC4 key scheduler cycles short keys, which means that | ||
return (options.pass || callback || | ||
// If called as a method of Math (Math.seedrandom()), mutate Math.random | ||
// because that is how seedrandom.js has worked since v1.0. Otherwise, | ||
// it is a newer calling convention, so return the prng directly. | ||
function(prng, seed, is_math_call) { | ||
function(prng, seed, is_math_call, state) { | ||
if (state) { | ||
// Load the arc4 state from the given state if it has an S array. | ||
if (state.S) { copy(state, arc4); } | ||
// Only provide the .state method if requested via options.state. | ||
prng.state = function() { return copy(arc4, {}); } | ||
} | ||
// If called as a method of Math (Math.seedrandom()), mutate | ||
// Math.random because that is how seedrandom.js has worked since v1.0. | ||
if (is_math_call) { math[rngname] = prng; return seed; } | ||
// Otherwise, it is a newer calling convention, so return the | ||
// prng directly. | ||
else return prng; | ||
@@ -271,3 +297,6 @@ })( | ||
return (n + x) / d; // Form the number within [0, 1). | ||
}, shortseed, 'global' in options ? options.global : (this == math)); | ||
}, | ||
shortseed, | ||
'global' in options ? options.global : (this == math), | ||
options.state); | ||
}; | ||
@@ -320,2 +349,13 @@ | ||
// | ||
// copy() | ||
// Copies internal state of ARC4 to or from a plain object. | ||
// | ||
function copy(f, t) { | ||
t.i = f.i; | ||
t.j = f.j; | ||
t.S = f.S.slice(); | ||
return t; | ||
}; | ||
// | ||
// flatten() | ||
@@ -322,0 +362,0 @@ // Converts an object tree to nested arrays of strings. |
@@ -1,1 +0,1 @@ | ||
!function(a,b,c,d,e,f,g,h,i){function j(a){var b,c=a.length,e=this,f=0,g=e.i=e.j=0,h=e.S=[];for(c||(a=[c++]);d>f;)h[f]=f++;for(f=0;d>f;f++)h[f]=h[g=s&g+a[f%c]+(b=h[f])],h[g]=b;(e.g=function(a){for(var b,c=0,f=e.i,g=e.j,h=e.S;a--;)b=h[f=s&f+1],c=c*d+h[s&(h[f]=h[g=s&g+b])+(h[g]=b)];return e.i=f,e.j=g,c})(d)}function k(a,b){var c,d=[],e=typeof a;if(b&&"object"==e)for(c in a)try{d.push(k(a[c],b-1))}catch(f){}return d.length?d:"string"==e?a:a+"\0"}function l(a,b){for(var c,d=a+"",e=0;e<d.length;)b[s&e]=s&(c^=19*b[s&e])+d.charCodeAt(e++);return n(b)}function m(c){try{return o?n(o.randomBytes(d)):(a.crypto.getRandomValues(c=new Uint8Array(d)),n(c))}catch(e){return[+new Date,a,(c=a.navigator)&&c.plugins,a.screen,n(b)]}}function n(a){return String.fromCharCode.apply(0,a)}var o,p=c.pow(d,e),q=c.pow(2,f),r=2*q,s=d-1,t=c["seed"+i]=function(a,f,g){var h=[];f=1==f?{entropy:!0}:f||{};var o=l(k(f.entropy?[a,n(b)]:null==a?m():a,3),h),s=new j(h);return l(n(s.S),b),(f.pass||g||function(a,b,d){return d?(c[i]=a,b):a})(function(){for(var a=s.g(e),b=p,c=0;q>a;)a=(a+c)*d,b*=d,c=s.g(1);for(;a>=r;)a/=2,b/=2,c>>>=1;return(a+c)/b},o,"global"in f?f.global:this==c)};if(l(c[i](),b),g&&g.exports){g.exports=t;try{o=require("crypto")}catch(u){}}else h&&h.amd&&h(function(){return t})}(this,[],Math,256,6,52,"object"==typeof module&&module,"function"==typeof define&&define,"random"); | ||
!function(a,b,c,d,e,f,g,h,i){function j(a){var b,c=a.length,e=this,f=0,g=e.i=e.j=0,h=e.S=[];for(c||(a=[c++]);d>f;)h[f]=f++;for(f=0;d>f;f++)h[f]=h[g=t&g+a[f%c]+(b=h[f])],h[g]=b;(e.g=function(a){for(var b,c=0,f=e.i,g=e.j,h=e.S;a--;)b=h[f=t&f+1],c=c*d+h[t&(h[f]=h[g=t&g+b])+(h[g]=b)];return e.i=f,e.j=g,c})(d)}function k(a,b){return b.i=a.i,b.j=a.j,b.S=a.S.slice(),b}function l(a,b){var c,d=[],e=typeof a;if(b&&"object"==e)for(c in a)try{d.push(l(a[c],b-1))}catch(f){}return d.length?d:"string"==e?a:a+"\0"}function m(a,b){for(var c,d=a+"",e=0;e<d.length;)b[t&e]=t&(c^=19*b[t&e])+d.charCodeAt(e++);return o(b)}function n(c){try{return p?o(p.randomBytes(d)):(a.crypto.getRandomValues(c=new Uint8Array(d)),o(c))}catch(e){return[+new Date,a,(c=a.navigator)&&c.plugins,a.screen,o(b)]}}function o(a){return String.fromCharCode.apply(0,a)}var p,q=c.pow(d,e),r=c.pow(2,f),s=2*r,t=d-1,u=c["seed"+i]=function(a,f,g){var h=[];f=1==f?{entropy:!0}:f||{};var p=m(l(f.entropy?[a,o(b)]:null==a?n():a,3),h),t=new j(h);return m(o(t.S),b),(f.pass||g||function(a,b,d,e){return e&&(e.S&&k(e,t),a.state=function(){return k(t,{})}),d?(c[i]=a,b):a})(function(){for(var a=t.g(e),b=q,c=0;r>a;)a=(a+c)*d,b*=d,c=t.g(1);for(;a>=s;)a/=2,b/=2,c>>>=1;return(a+c)/b},p,"global"in f?f.global:this==c,f.state)};if(m(c[i](),b),g&&g.exports){g.exports=u;try{p=require("crypto")}catch(v){}}else h&&h.amd&&h(function(){return u})}(this,[],Math,256,6,52,"object"==typeof module&&module,"function"==typeof define&&define,"random"); |
var assert = require("assert"); | ||
var seedrandom = require("../seedrandom"); | ||
var requirejs = require("requirejs"); | ||
requirejs.config({ | ||
baseUrl: __dirname | ||
}); | ||
describe("Nodejs API Test", function() { | ||
var original = Math.random, | ||
result, r, xprng, obj, as2, as3, autoseed1, myrng, | ||
firstprng, secondprng, thirdprng; | ||
it('should pass basic tests.', function() { | ||
var original = Math.random, | ||
result, r, xprng, obj, as2, as3, autoseed1, myrng, | ||
firstprng, secondprng, thirdprng; | ||
it('should pass basic tests.', function() { | ||
result = Math.seedrandom('hello.'); | ||
@@ -34,3 +38,3 @@ firstprng = Math.random; | ||
it('should be able to add entropy.'); | ||
// should be able to add entropy. | ||
result = Math.seedrandom('added entropy.', { entropy:true }); | ||
@@ -169,4 +173,50 @@ assert.equal(result.length, 256, "Should return short seed."); | ||
it('should support state api.', function() { | ||
// Verify that there is no state method | ||
var dummy = seedrandom('hello'); | ||
var unexpected = -1; | ||
var expected = -1; | ||
try { | ||
unexpected = dummy.state(); | ||
} catch(e) { | ||
expected = 1; | ||
} | ||
assert.equal(unexpected, -1); | ||
assert.equal(expected, 1); | ||
var count = 0; | ||
for (var x in dummy) { | ||
count += 1; | ||
} | ||
assert.equal(count, 0); | ||
// Verify that a state method can be added | ||
var saveable = seedrandom("secret-seed", {state: true}); | ||
var ordinary = seedrandom("secret-seed"); | ||
for (var j = 0; j < 1e2; ++j) { | ||
assert.equal(ordinary(), saveable()); | ||
} | ||
var virgin = seedrandom("secret-seed"); | ||
var saved = saveable.state(); | ||
var replica = seedrandom("", {state: saved}); | ||
for (var j = 0; j < 1e2; ++j) { | ||
var r = replica(); | ||
assert.equal(r, saveable()); | ||
assert.equal(r, ordinary()); | ||
assert(r != virgin()); | ||
} | ||
}); | ||
it('should support requirejs in node.', function() { | ||
var original = Math.random; | ||
var rsr = requirejs('../seedrandom'); | ||
var rng = rsr('hello.'); | ||
assert.equal(typeof(rng), 'function', "Should return a function."); | ||
var r = rng(); | ||
assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1"); | ||
assert(original === Math.random, "Should not change Math.random."); | ||
assert(original !== rng, "PRNG should not be Math.random."); | ||
}); | ||
// End of test. | ||
}); |
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
208762
23
4740
241
12