diff options
| author | Marak <[email protected]> | 2014-09-11 18:24:06 +0200 |
|---|---|---|
| committer | Marak <[email protected]> | 2014-09-11 18:28:30 +0200 |
| commit | 14b42846fef60bb0d1493a561d0b9aaa40a95bfc (patch) | |
| tree | 112a8fa8950553983ed098f7460482c1302a46a4 /lib/random.js | |
| parent | 198032b5d37a4882fc7d19d273475bb087685f2e (diff) | |
| download | faker-14b42846fef60bb0d1493a561d0b9aaa40a95bfc.tar.xz faker-14b42846fef60bb0d1493a561d0b9aaa40a95bfc.zip | |
[api] [refactor] Replaced Math.Random calls with Random.number. Random ranges are now inclusive of max instead of exclusive. #88 #48
Diffstat (limited to 'lib/random.js')
| -rw-r--r-- | lib/random.js | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/lib/random.js b/lib/random.js index 7e8cca37..027d6eb5 100644 --- a/lib/random.js +++ b/lib/random.js @@ -3,17 +3,40 @@ var faker = require('../index'); var random = { // returns a single random number based on a max number or range - number: function (min, max) { - if (max === undefined) { - max = min; - min = 0; + number: function (options) { + + if (typeof options === "number") { + var options = { + max: options + }; + } + + options = options || { + min: 0, + max: 1, + precision: 1 + }; + + if (typeof options.min === "undefined") { + options.min = 0; + } + + 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++; } - return Math.floor((Math.random() * max) + min); + + return Math.floor(Math.random() * (options.max - options.min)) + options.min; + }, // takes an array and returns the array randomly sorted array_element: function (array) { - var r = Math.floor(Math.random() * array.length); + var r = faker.random.number({ max: array.length -1 }); return array[r]; }, |
