Math.random()

Math.random() 静态方法返回一个大于或等于 0 且小于 1 的浮点伪随机数,在该范围内近似均匀分布 — 然后你可以将其缩放到所需的范围。该实现选择随机数生成算法的初始种子;用户无法选择或重置它。

¥The Math.random() static method returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.

注意:Math.random() 不提供加密安全随机数。请勿将它们用于与安全相关的任何事情。请改用 Web Crypto API,更准确地说是 window.crypto.getRandomValues() 方法。

¥Note: Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.

Try it

语法

¥Syntax

js
Math.random()

参数

¥Parameters

没有任何。

¥None.

返回值

¥Return value

介于 0(含)和 1(不含)之间的浮点伪随机数。

¥A floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive).

示例

¥Examples

请注意,由于 JavaScript 中的数字是具有舍入到最接近偶数行为的 IEEE 754 浮点数,因此以下函数声明的范围(不包括 Math.random() 本身的范围)并不准确。如果选择极大的边界(253 或更高),则在极少数情况下有可能达到通常排除的上限。

¥Note that as numbers in JavaScript are IEEE 754 floating point numbers with round-to-nearest-even behavior, the ranges claimed for the functions below (excluding the one for Math.random() itself) aren't exact. If extremely large bounds are chosen (253 or higher), it's possible in extremely rare cases to reach the usually-excluded upper bound.

获取 0(含)和 1(不含)之间的随机数

¥Getting a random number between 0 (inclusive) and 1 (exclusive)

js
function getRandom() {
  return Math.random();
}

获取两个值之间的随机数

¥Getting a random number between two values

此示例返回指定值之间的随机数。返回值不小于(且可能等于)min,且小于(且不等于)max

¥This example returns a random number between the specified values. The returned value is no lower than (and may possibly equal) min, and is less than (and not equal) max.

js
function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

获取两个值之间的随机整数

¥Getting a random integer between two values

此示例返回指定值之间的随机整数。该值不小于 min(如果 min 不是整数,则为大于 min 的下一个整数),并且小于(但不等于)max

¥This example returns a random integer between the specified values. The value is no lower than min (or the next integer greater than min if min isn't an integer), and is less than (but not equal to) max.

js
function getRandomInt(min, max) {
  const minCeiled = Math.ceil(min);
  const maxFloored = Math.floor(max);
  return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); // The maximum is exclusive and the minimum is inclusive
}

注意:使用 Math.round() 来实现这一点可能很诱人,但这样做会导致随机数遵循不均匀分布,这可能无法满足你的需求。

¥Note: It might be tempting to use Math.round() to accomplish that, but doing so would cause your random numbers to follow a non-uniform distribution, which may not be acceptable for your needs.

获取两个值之间的随机整数(包括两个值)

¥Getting a random integer between two values, inclusive

虽然上面的 getRandomInt() 函数在最小值处是包含的,但它在最大值处是排除的。如果你需要结果同时包含最小值和最大值怎么办?下面的 getRandomIntInclusive() 函数可以实现这一点。

¥While the getRandomInt() function above is inclusive at the minimum, it's exclusive at the maximum. What if you need the results to be inclusive at both the minimum and the maximum? The getRandomIntInclusive() function below accomplishes that.

js
function getRandomIntInclusive(min, max) {
  const minCeiled = Math.ceil(min);
  const maxFloored = Math.floor(max);
  return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled); // The maximum is inclusive and the minimum is inclusive
}

规范

Specification
ECMAScript Language Specification
# sec-math.random

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看