JavaScript Math.random()方法介绍

随机法 (Random Method)

The JavaScript Math.random() method is an excellent built-in method for producing random numbers. When Math.random() is executed, it returns a random number that can be anywhere between 0 and 1. The 0 is included and 1 is excluded.

JavaScript Math.random()方法是用于生成随机数的出色的内置方法。 执行Math.random() ,它将返回一个随机数,该随机数可以在0到1之间。包括0和不包括1。

生成介于0和1之间的随机浮点数 (Generating a random floating point number between 0 and 1)

The Math.random() method will return a floating point (decimal) number greater than or equal to 0 and less than (but never equal to) 1. In other words 0 <= x < 1. For example:

Math.random()方法将返回一个大于或等于0且小于(但从不等于)1的浮点数(十进制)。换句话说, 0 <= x < 1 。 例如:

console.log(Math.random());
// 0.7069207248635578

console.log(Math.random());
// 0.765046694794209

console.log(Math.random());
// 0.14069121642698246

(Of course, the numbers returned will be different every time. This will be assumed for all following examples - different results will happen on each pass.)

(当然,每次返回的数字都会有所不同。以下所有示例均将采用此假设-每次通过都会产生不同的结果。)

To get a random number between a larger range multiply the result of Math.random() by a number.

要获得较大范围内的随机数,请将Math.random()的结果乘以一个数字。

生成介于0和指定最大值之间的随机浮点数 (Generating a random floating point number between 0 and a specified max)

Usually you do not need random numbers between 0 and 1 - you need larger numbers or even integers.

通常,您不需要0到1之间的随机数-您需要更大的数字甚至整数。

For example, if you want a random floating point number between 0 and 10, you could use:

例如,如果您想要一个介于0到10之间的随机浮点数,则可以使用:

var x = Math.random()*10;

console.log(x);
// 4.133793901445541

生成范围内的随机浮点数 (Generating a random floating point number within a range)

If you need a random floating point number that ranges between two specific numbers, you could do something like this:

如果您需要一个介于两个特定数字之间的随机浮点数,则可以执行以下操作:

var min = 83.1;
var max = 193.36;

var x = Math.random()*(max - min)+min;

console.log(x);
// 126.94014012699063

生成介于0和最大值之间的随机整数 (Generating a random integer between 0 and a max)

Often you need integers. To do this you will have to use some other methods from the Math object, Math.floor() (rounds down to the nearest integer) and Math.ceil() (rounds up to the nearest integer).

通常,您需要整数。 为此,您将必须使用Math对象中的其他方法, Math.floor() (四舍五入到最接近的整数)和Math.ceil() (四舍五入到最接近的整数)。

For example, if you need to select randomly from an array of 10 elements, you would need a random number between 0 and 9 inclusive (remember that arrays are zero indexed).

例如,如果需要从10个元素的数组中随机选择,则需要一个0到9(含0和9)之间的随机数(请记住,数组的索引为零)。

var x = Math.floor(Math.random()*10);

console.log(x);
// 7

(Remember that Math.random() will never return exactly 1, so Math.random()*10 will never return exactly 10. This means that after rounding down, the result will always be 9 or less.)

(请记住, Math.random()永远不会精确返回1,因此Math.random()*10永远不会精确返回10。这意味着在四舍五入后,结果将始终为9或更少。)

生成介于1和最大之间的随机整数 (Generating a random integer between 1 and a max)

If you need a random number with the minimum number being 1 (for example picking a random day in January) you could use the Math.ceil() method.

如果您需要一个最小数为1的随机数(例如,在一月份选择一个随机日),则可以使用Math.ceil()方法。

var x = Math.ceil(Math.random()*31);

console.log(x);
// 23

Another way would have been to use the previous function (using Math.floor()) and add 1 to it:

另一种方法是使用先前的函数(使用Math.floor() )并向其中添加1:

var x = Math.floor(Math.random()*31)+1;

console.log(x);
// 17

生成范围内的随机整数 (Generating a random integer within a range)

Lastly, occasionally you need a random integer between two specific integers. For example, if you are trying to pick raffle tickets and you know the numbers of the lowest and largest number:

最后,有时您需要在两个特定整数之间的随机整数。 例如,如果您尝试领取抽奖券,并且知道最低和最大号码:

var min = 1718;
var max = 3429;

var x = Math.floor(Math.random()*(max-min+1)+min);

console.log(x);
//2509

Math.random()的随机性如何? (How random is Math.random()?)

It may be pointed out that the number returned by Math.random() is a pseudo-random number as no computer can generate a truly random number, that exhibits randomness over all scales and over all sizes of data sets. However, the pseudo-random number generated by Math.random() is usually sufficient for the needs of nearly any program you may write. The not-truly-randomness only becomes apparent in astronomically large number sets or when uncommonly precise decimals are needed.

可能会指出Math.random()返回的数字是伪随机数,因为没有计算机能够生成真正的随机数,该随机数在所有规模和所有大小的数据集上都表现出随机性。 但是, Math.random()生成的伪随机数通常足以满足您可能编写的几乎所有程序的需要。 只有在天文数字很大的集合中或在需要非常不精确的小数位时,才会出现非真正的随机性。

翻译自: https://www.freecodecamp.org/news/javascript-math-random-method-explained/

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值