Math.random()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

函數 Math.random() 會回傳一個介於 0 到 1 之間(包含 0,不包含 1)的偽隨機(pseudo-random)小數 ,大致符合數學與統計上的均勻分佈 (uniform distribution) ,你可以選定想要的數字區間,它會透過演算法被產生並且不允許使用者自行跳選或重設成特定數字。

備註: Math.random() 所產生的偽隨機小數不符合加密學安全性要求。請勿使用於任何加密、資安相關領域。如有加密需求建議參考 Web Crypto API 的 Crypto.getRandomValues() 方法。

嘗試一下

function getRandomInt(max) {
  return Math.floor(Math.random() * max);
}

console.log(getRandomInt(3));
// Expected output: 0, 1 or 2

console.log(getRandomInt(1));
// Expected output: 0

console.log(Math.random());
// Expected output: a number from 0 to <1

語法

js
Math.random()

回傳值

回傳一個偽隨機浮點數,介於 0 到 1 之間(包含 0,不包含 1)。

範例

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. Usually, the claimed upper bound is not attainable, but if Math.random() returns a number very close to 1, the tiny difference may not be representable at the requested maximum, therefore causing the upper bound to be attained.

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

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

Getting a random number between two values

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

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
}

備註: 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

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® 2025 Language Specification
# sec-math.random

瀏覽器相容性

參見