Skip to content

Commit

Permalink
Merge branch 'pr-1900' into devel
Browse files Browse the repository at this point in the history
  • Loading branch information
n1mmy committed Mar 21, 2014
2 parents fc3cbd6 + f2a2b2e commit 5b4ecd9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/random/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ RandomGenerator.prototype.fraction = function () {
var array = new Uint32Array(1);
window.crypto.getRandomValues(array);
return array[0] * 2.3283064365386963e-10; // 2^-32
} else {
throw new Error('No random generator available');
}
};

Expand Down Expand Up @@ -187,6 +189,9 @@ if (nodeCrypto ||
else
Random = new RandomGenerator([new Date(), height, width, agent, Math.random()]);

Random.create = function () {
Random.createWithSeeds = function () {
if (arguments.length === 0) {
throw new Error('No seeds were provided');
}
return new RandomGenerator(arguments);
};
19 changes: 18 additions & 1 deletion packages/random/random_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Tinytest.add('random', function (test) {
// algorithm being used and it starts generating a different
// sequence for a seed, as long as the sequence is consistent for
// a particular release.
var random = Random.create(0);
var random = Random.createWithSeeds(0);
test.equal(random.id(), "cp9hWvhg8GSvuZ9os");
test.equal(random.id(), "3f3k6Xo7rrHCifQhR");
test.equal(random.id(), "shxDnjWWmnKPEoLhM");
Expand All @@ -27,3 +27,20 @@ Tinytest.add('random - format', function (test) {
test.isTrue(frac < 1.0);
test.isTrue(frac >= 0.0);
});

Tinytest.add('random - Alea is last resort', function (test) {
if (Meteor.isServer) {
test.isTrue(Random.alea === undefined);
}
if (Meteor.isClient) {
var useGetRandomValues = !!(typeof window !== "undefined" &&
window.crypto && window.crypto.getRandomValues);
test.equal(Random.alea === undefined, useGetRandomValues);
}
});

Tinytest.add('random - createWithSeeds requires parameters', function (test) {
test.throws(function () {
Random.createWithSeeds();
});
});
2 changes: 1 addition & 1 deletion packages/test-helpers/seeded_random.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SeededRandom = function(seed) { // seed may be a string or any type
return new SeededRandom(seed);

seed = seed || "seed";
this.gen = Random.create(seed).alea; // from random.js
this.gen = Random.createWithSeeds(seed).alea; // from random.js
};
SeededRandom.prototype.next = function() {
return this.gen();
Expand Down

0 comments on commit 5b4ecd9

Please sign in to comment.