K20GH
K20GH

Reputation: 6281

Faker.js random number between 2 values

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.

Upvotes: 71

Views: 131732

Answers (6)

Greg Wozniak
Greg Wozniak

Reputation: 7262

In the newer versions (8.4.1, August 2024), faker.random is deprecated. You need to use:

faker.helpers.rangeToNumber({min: 10, max: 50})}

Upvotes: 0

cre8
cre8

Reputation: 13562

Update 2023 for v8

Now moved to https://github.com/faker-js/faker, they changed it again:

faker.number.int(options: number | {
  max: number,
  min: number
} = {}): number
faker.number.int() // 2900970162509863
faker.number.int(100) // 52
faker.number.int({ min: 1000000 }) // 2900970162509863
faker.number.int({ max: 100 }) // 42
faker.number.int({ min: 10, max: 100 }) // 57

source: https://fakerjs.dev/api/number.html#int

Update 2021

Latest versions changed location of the function from faker.random.number to faker.datatype.number, https://github.com/Marak/faker.js/issues/1156

Original answer

You need to give an object to the function:

faker.datatype.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;

  }

Upvotes: 124

Alex Quintero
Alex Quintero

Reputation: 1179

My solution was to use this library: https://github.com/faker-js/faker

and in addition to that, it creates these methods that it needed and that the library does not currently support.

export function getCreditCardBrand(): string {
  const brands = [
    'Visa',
    'MasterCard',
    'American Express',
    'Discover',
    'JCB',
    'Diners Club',
  ];
  const randomIndex = Math.floor(Math.random() * brands.length);
  return brands[randomIndex];
}

export function getBoolean(): boolean {
  const booleans = [true, false];
  const randomIndex = Math.floor(Math.random() * booleans.length);
  return booleans[randomIndex];
}

export function getRandomElement<T>(items: T[]): T {
  return items[Math.floor(Math.random() * items.length)];
}

interface NumberRange {
  min: number;
  max: number;
}

export function getRandomNumber(range: NumberRange): number {
  return Math.floor(Math.random() * (range.max - range.min + 1)) + range.min;
}

Upvotes: 0

Penny Liu
Penny Liu

Reputation: 17468

I encountered a warning while running the faker.random.number function in my application using [email protected].

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

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

faker.datatype.number(100); // return 88
faker.datatype.number({ min: 5, max: 10 }); // return 7

Upvotes: 11

Craicerjack
Craicerjack

Reputation: 6332

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

Upvotes: 43

Edwin O.
Edwin O.

Reputation: 5276

try passing a hash like below

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

Upvotes: 3

Related Questions