Javascript Faker.js 2 个值之间的随机数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32010910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:30:49  来源:igfitidea点击:

Faker.js random number between 2 values

javascriptfaker

提问by K20GH

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

这个让我有点生气,我相信它很简单,但似乎没有任何地方记录。

Im using https://github.com/marak/Faker.js/and the following to generate my random number:

我使用https://github.com/marak/Faker.js/和以下内容来生成我的随机数:

faker.random.number();

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

效果很好,现在如果我想在 2 个数字之间进行,我该怎么做?

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

然而,这只是给了我从 0 到 10 的数字。不知道 50 在做什么

Can anyone give me some directions to this please

任何人都可以给我一些指示吗

回答by cre8

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.

因此,如果您只传递一个数字,它会将其设置为最大值。最小值默认为 0。

This is the implementation of the number function :

这是 number 函数的实现:

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;

  }

回答by CraicerHyman

From Fakerjs github

来自Fakerjs github

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

整数伪造者。random.number(min,max)0 和“范围”之间的随机数。

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.

十进制数伪造者。Finance.amount(min,max,decimal Places)“min”和“max”之间的随机数,包括小数到 X 位。

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

Boolean faker.random.boolean()

布尔伪造者。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.

数组元素伪造者。random.arrayElement(array[])从可能值的数组中选择一个随机元素。此功能可用于创建自定义可能性列表。

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.

对象元素伪造者。random.objectElement(object{})从对象中选择一个随机元素,选择值而不是键。此功能可用于创建自定义可能性列表。

faker.random.objectElement({one: 1, two: 2, three: 3}); //returns 3

回答by Edwin Ikechukwu Okonkwo

try passing a hash like below

尝试传递如下所示的哈希值

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