aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMarak <[email protected]>2014-11-29 19:40:56 +0530
committerMarak <[email protected]>2014-11-29 19:40:56 +0530
commit0370b2eedb2fa667716308d42961a3bc48b933a0 (patch)
tree0adfe917f4c461dcdf9a22eaaecf6f0ee429688a /lib
parent11fefd8006a8a2d63bcb02ad0f2bf213716e34a9 (diff)
parent89070a9b5a544801eeacd6f3c5fea83794a42570 (diff)
downloadfaker-0370b2eedb2fa667716308d42961a3bc48b933a0.tar.xz
faker-0370b2eedb2fa667716308d42961a3bc48b933a0.zip
Merge pull request #146 from mkawalec/master
[fix] Precision changes precision and max doesn't modify options object
Diffstat (limited to 'lib')
-rw-r--r--lib/random.js24
1 files changed, 13 insertions, 11 deletions
diff --git a/lib/random.js b/lib/random.js
index 70bcaf04..a4bc1858 100644
--- a/lib/random.js
+++ b/lib/random.js
@@ -6,16 +6,12 @@ var random = {
number: function (options) {
if (typeof options === "number") {
- var options = {
+ options = {
max: options
};
}
- options = options || {
- min: 0,
- max: 1,
- precision: 1
- };
+ options = options || {};
if (typeof options.min === "undefined") {
options.min = 0;
@@ -24,13 +20,19 @@ var random = {
if (typeof options.max === "undefined") {
options.max = 1;
}
-
- // by incrementing max by 1, max becomes inclusive of the range
- if (options.max > 0) {
- options.max++;
+ if (typeof options.precision === "undefined") {
+ options.precision = 1;
}
- var randomNumber = mersenne.rand(options.max, options.min);
+ // Make the range inclusive of the max value
+ var max = options.max;
+ if (max > 0) {
+ max += options.precision;
+ }
+
+ var randomNumber = options.precision * Math.floor(
+ mersenne.rand(max / options.precision, options.min / options.precision));
+
return randomNumber;
},