Faker.js 2个值之间的随机数 [英] 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.
我正在使用 Faker.js 和以下内容生成我的随机数:>
Im using Faker.js and the following to generate my random number:
faker.random.number();
很好,现在,如果我想在2个数字之间进行运算,我将如何处理?
Works great, now if I want to do it between 2 numbers, how would I go about this?
我尝试了以下操作:
faker.random.number(10, 50);
但是,这只能为我提供从 0
到 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.
推荐答案
您需要为该函数提供一个对象:
You need to give an object to the function:
faker.random.number({
'min': 10,
'max': 50
});
因此,如果您仅传递数字,它将设置为最大值.最小值默认为0.
So if you just pass a number, it will set it as the max value. The min value is 0 by default.
这是数字功能的实现:
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;
}
这篇关于Faker.js 2个值之间的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!