diff --git a/lib/random.js b/lib/random.js index d5f112ce0..17b74f647 100644 --- a/lib/random.js +++ b/lib/random.js @@ -47,8 +47,10 @@ function Random (faker, seed) { max += options.precision; } - var randomNumber = options.precision * Math.floor( + var randomNumber = Math.floor( 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); return randomNumber; diff --git a/test/random.unit.js b/test/random.unit.js index 6214039b2..133fd8870 100644 --- a/test/random.unit.js +++ b/test/random.unit.js @@ -56,6 +56,14 @@ describe("random.js", function () { }); + it("provides numbers with a with exact precision", function() { + var options = { min: 0.5, max: 0.99, precision: 0.01 }; + for(var i = 0; i < 100; i++) { + var number = faker.random.number(options); + assert.equal(number, Number(number.toFixed(2))); + } + }); + it("should not modify the input object", function() { var min = 1; var max = 2;