最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

SEO心得admin51浏览0评论
本文介绍了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; }
发布评论

评论列表(0)

  1. 暂无评论