33

This one is driving me a little mad, I'm sure it's simple but it doesn't seem to be documented anywhere.

Im using Faker.js and the following to generate my random number:

faker.random.number();

Works great, now if I want to do it between 2 numbers, how would I go about this?

I tried the following:

faker.random.number(10, 50);

However, that just gives me numbers from 0 to 10. No idea what the 50 is doing.

Can anyone give me some directions to this please.

Penny Liu
  • 7,720
  • 5
  • 40
  • 66
K20GH
  • 4,944
  • 16
  • 60
  • 94

4 Answers4

63

You need to give an object to the function:

faker.random.number({
    'min': 10,
    'max': 50
});

So if you just pass a number, it will set it as the max value. The min value is 0 by default.

This is the implementation of the number function :

this.number = function (options) {

    if (typeof options === "number") {
      options = {
        max: options
      };
    }

    options = options || {};

    if (typeof options.min === "undefined") {
      options.min = 0;
    }

    if (typeof options.max === "undefined") {
      options.max = 99999;
    }
    if (typeof options.precision === "undefined") {
      options.precision = 1;
    }

    // Make the range inclusive of the max value
    var max = options.max;
    if (max >= 0) {
      max += options.precision;
    }

    var randomNumber = options.precision * Math.floor(
      mersenne.rand(max / options.precision, options.min / options.precision));

    return randomNumber;

  }
Edwin O.
  • 3,671
  • 33
  • 37
cre8
  • 11,108
  • 5
  • 33
  • 55
  • 6
    The fact that you can pass in params in this way is neither mentioned [in the demo page](https://cdn.rawgit.com/Marak/faker.js/master/examples/browser/index.html#random), nor in the [API docs](http://marak.github.io/faker.js/faker.random.html#-static-number__anchor) – Jayant Bhawal Sep 11 '18 at 05:46
  • 1
    The newest version is now changed, instead of `faker.random`, one should use `faker.datatype` – Frederiko Cesar May 21 '21 at 08:29
  • DEPRECATED syntax above – Hos Mercury May 29 '21 at 01:35
29

From Fakerjs github

Whole Number faker. random.number(min,max) Random number between 0 and "range".

faker.random.number(100); //returns 92
faker.random.number({min:5, max:10}); //returns 9

Decimal number faker. finance.amount(min,max,decimal places) Random number between "min" and "max" including decimals to X digits.

faker.finance.amount(9000,10000,4); //returns 9948.8363

Boolean faker. random.boolean()

faker.random.boolean(); //returns true

Array Element faker. random.arrayElement(array[]) Selects a random element from an array of possible values. This function is useful to create custom lists of possibilities.

faker.random.arrayElement(["one","two","three","four"]); //returns "two"
var phTyp = faker.random.arrayElement(["cell","work","home"]); //returns "work"

Object Element faker. random.objectElement(object{}) Selects a random element from an object, selects the value not the keys. This function is useful to create custom lists of possibilities.

faker.random.objectElement({one: 1, two: 2, three: 3}); //returns 3
Russ Matney
  • 1,145
  • 12
  • 16
Craicerjack
  • 5,524
  • 2
  • 27
  • 36
3

I encounter this warning while running faker.random.number in my application faker@5.5.3.

Deprecation Warning: faker.random.number is now located in faker.datatype.number

The methods have been moved. A better fix would be replaced with faker.datatype.number().

faker.datatype.number(100); // return 88
faker.datatype.number({ min: 5, max: 10 }); // return 7
Penny Liu
  • 7,720
  • 5
  • 40
  • 66
0

try passing a hash like below

faker.random.number({ min: 10, max: 50})
Edwin O.
  • 3,671
  • 33
  • 37