diff --git a/README.md b/README.md index b23396f..01ac51a 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,34 @@ Selects a random element from provided array. fake.fromArray(['banana', 'apple', 'strawberry', 'raspberry', 'pear']); // 'pear' +  + +  + +### `Fake.integer([[min,]max])` + +Generate a random integer. If no argument passed, the default output is a random integer between 0 to 1000. + +*Example:* + + Fake.integer(); + // 476 + Fake.integer(100); + // 23 + Fake.integer(5,10); + // 6 + +### `Fake.float([[min,]max])` + +Generate a random float. If no argument passed, the default output is a random float between 0 to 1000. + +*Example:* + Fake.float(); + // 682.4292875826359 + Fake.float(100); + // 18.00180042628199 + Fake.float(5,10); + // 8.542717032833025 diff --git a/fake.js b/fake.js index 477fef6..20909d2 100644 --- a/fake.js +++ b/fake.js @@ -184,6 +184,49 @@ var defaultUserFields = ['name', 'surname', 'fullname', 'email']; Fake = {}; +Fake.integer = function(a,b){ + /* ---------- ---------- ---------- ---------- ---------- ---------- */ + /* Usage: + /* Fake.integer() // 0 ~ 1000 + /* Fake.integer(max) // 0 ~ max + /* Fake.integer(min,max) // min ~ max + /* ---------- ---------- ---------- ---------- ---------- ---------- */ + var max = 0; + var min = 0; + if(a && b){ + max=b; + min=a; + }else if (a){ + max=a; + min=0; + }else{ + max=1000; + min=0; + } + return Math.floor(Math.random() * (max - min + 1)) + min; +} + +Fake.float = function(a,b){ + /* ---------- ---------- ---------- ---------- ---------- ---------- */ + /* Usage: + /* Fake.float() // 0 ~ 1000 + /* Fake.float(max) // 0 ~ max + /* Fake.float(min,max) // min ~ max + /* ---------- ---------- ---------- ---------- ---------- ---------- */ + var max = 0; + var min = 0; + if(a && b){ + max=b; + min=a; + }else if (a){ + max=a; + min=0; + }else{ + max=1000; + min=0; + } + return Math.random() * (max - min) + min; +} Fake.user = function(params) {