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

编程入门 行业动态 更新时间:2024-10-08 10:52:50
本文介绍了Faker.js 2个值之间的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这使我发疯了,我敢肯定这很简单,但似乎没有任何地方记录在案.

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个值之间的随机数

本文发布于:2023-11-30 15:37:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650532.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:随机数   Faker   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!