aboutsummaryrefslogtreecommitdiff
path: root/lib/random.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/random.js')
-rw-r--r--lib/random.js35
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];
},