| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/random | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.
Math.random() 0 1 , . , .
:
Math.random() . . Web Crypto API, window.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
Math.random()
.
0 1 .
JavaScript (number) (round-to-nearest-even behavior) IEEE 754 , (Math.random() ) . .
function getRandom() {
return Math.random();
}
. min( min min ) max .
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
. min(, min min ) , max .
function getRandomInt(min, max) {
const minCeiled = Math.ceil(min);
const maxFloored = Math.floor(max);
return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); // ,
}
:
Math.round() , .
getRandomInt() , . , getRandomIntInclusive() .
function getRandomIntInclusive(min, max) {
const minCeiled = Math.ceil(min);
const maxFloored = Math.floor(max);
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled); // ,
}
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-math.random |
This page was last modified on 2025 10 9 by MDN contributors.
MathMath.abs()Math.acos()Math.acosh()Math.asin()Math.asinh()Math.atan()Math.atan2()Math.atanh()Math.cbrt()Math.ceil()Math.clz32()Math.cos()Math.cosh()Math.exp()expm1()Math.f16round()Math.floor()Math.fround()hypot()Math.imul()Math.log()log1p()Math.log2()Math.log10()Math.max()Math.min()Math.pow()Math.random()Math.round()Math.sign()Math.sin()Math.sinh()Math.sqrt()sumPrecise()Math.tan()Math.tanh()Math.trunc()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |