aboutsummaryrefslogtreecommitdiff
path: root/lib/random.js
diff options
context:
space:
mode:
authorMichał Kawalec <[email protected]>2014-11-28 14:43:05 +0100
committerMichał Kawalec <[email protected]>2014-11-28 14:43:05 +0100
commit03e10c4475676bc9799cfe82bb5b6f12cfefe9e3 (patch)
tree6d19316c424bcd8dd6f30a1e1763577b518c328c /lib/random.js
parent11fefd8006a8a2d63bcb02ad0f2bf213716e34a9 (diff)
downloadfaker-03e10c4475676bc9799cfe82bb5b6f12cfefe9e3.tar.xz
faker-03e10c4475676bc9799cfe82bb5b6f12cfefe9e3.zip
precision changes precision and max doesn't modify options object
Diffstat (limited to 'lib/random.js')
-rw-r--r--lib/random.js23
1 files changed, 13 insertions, 10 deletions
diff --git a/lib/random.js b/lib/random.js
index 70bcaf04..5606622c 100644
--- a/lib/random.js
+++ b/lib/random.js
@@ -11,11 +11,7 @@ var random = {
};
}
- options = options || {
- min: 0,
- max: 1,
- precision: 1
- };
+ options = options || {};
if (typeof options.min === "undefined") {
options.min = 0;
@@ -24,13 +20,20 @@ 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 = Math.floor(options.precision *
+ mersenne.rand(max / options.precision,
+ options.min / options.precision)
+ );
return randomNumber;
},