Skip to content

Commit

Permalink
precision changes precision and max doesn't modify options object
Browse files Browse the repository at this point in the history
  • Loading branch information
mkawalec committed Nov 28, 2014
1 parent 11fefd8 commit 03e10c4
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ var random = {
};
}

options = options || {
min: 0,
max: 1,
precision: 1
};
options = options || {};

if (typeof options.min === "undefined") {
options.min = 0;
Expand All @@ -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;

},
Expand Down

0 comments on commit 03e10c4

Please sign in to comment.