Skip to content

Commit

Permalink
Revert "Update Random to create a per-instance copy of the RNG"
Browse files Browse the repository at this point in the history
This reverts commit 8a33cf0.
  • Loading branch information
Marak committed Sep 9, 2017
1 parent 454c915 commit b198dda
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 31 deletions.
18 changes: 3 additions & 15 deletions lib/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,15 @@ var mersenne = require('../vendor/mersenne');
* @namespace faker.random
*/
function Random (faker, seed) {
var gen = new mersenne.MersenneTwister19937();

// Use a user provided seed if it exists
if (seed) {
if (Array.isArray(seed) && seed.length) {
gen.init_by_array(seed);
mersenne.seed_array(seed);
}
else {
gen.init_genrand(seed);
mersenne.seed(seed);
}
}

// Copied from the marsenne package for compatibility.
function rand(max, min) {
if (max === undefined) {
min = 0;
max = 32768;
}
return Math.floor(gen.genrand_real2() * (max - min) + min);
}

/**
* returns a single random number based on a max number or range
*
Expand Down Expand Up @@ -60,7 +48,7 @@ function Random (faker, seed) {
}

var randomNumber = Math.floor(
rand(max / options.precision, options.min / options.precision));
mersenne.rand(max / options.precision, options.min / options.precision));
// Workaround problem in Float point arithmetics for e.g. 6681493 / 0.01
randomNumber = randomNumber / (1 / options.precision);

Expand Down
16 changes: 0 additions & 16 deletions test/random.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ if (typeof module !== 'undefined') {
var sinon = require('sinon');
var _ = require('lodash');
var faker = require('../index');
var Faker = require('../lib');
}


Expand Down Expand Up @@ -190,19 +189,4 @@ describe("random.js", function () {
assert.ok(hex.match(/^(0x)[0-9a-f]+$/i));
})
})

describe('independent', function() {

it('generates independent sequences', function () {
var faker1 = new Faker();
faker1.seed(1);

var faker2 = new Faker();
faker2.seed(1);

assert.equal(faker1.random.number(), faker2.random.number());
})

})

});

0 comments on commit b198dda

Please sign in to comment.