Home JavaScript Math Methods random() JavaScript Math.random() Method Definition Math.random() returns a random decimal number from 0 up to, but not including , 1. Multiply and round the result to get whole numbers in the range you want.
Read the syntax below, then try the three examples to see the method in action.
Syntax Call the method on Math. Start with this line.
Call What it does Math.random()Returns a random number from 0 (included) to 1 (not included). Math.floor(Math.random() * n)Random whole number from 0 to n - 1.
Built into JavaScript. Call Math.random(...) on the Math object — no import required. Math is not a constructor, so never write new Math().
Library declarations Parameters None. Math.random() takes no arguments.
Return Value Return Value Description Number A random decimal that is at least 0 and always less than 1.
JavaScript Examples 1. Range 0 to 1 2. Roll a dice 3. Range and array item Example 1 — Random Number from 0 to 1 Each call gives a new decimal. We check the range instead of printing it, because the number changes every time.
const n = Math.random();
console.log(n >= 0); // true
console.log(n < 1); // true
console.log(typeof n); // number How It Works New number each time. Math.random() returns something like 0.7318.... It is different on every call.Never negative. The value is always 0 or more.Never 1. The value is always smaller than 1.Type. The result is a normal number.Example 2 — Roll a Dice (1 to 6) Multiply by 6, drop the decimal with Math.floor(), then add 1. Your number will change every time you run it.
const dice = Math.floor(Math.random() * 6) + 1;
console.log("You rolled: " + dice); How It Works Scale. Math.random() * 6 gives a decimal from 0 up to just below 6.Cut the decimal. Math.floor() turns it into 0, 1, 2, 3, 4 or 5.Shift by one. Adding 1 makes the range 1 to 6.Different each run. The sample output shows 4, but you may get any number from 1 to 6.Example 3 — Random Whole Number in a Range and Random Array Item A small helper returns a whole number between min and max (both included). We test it 1000 times to prove it stays in range.
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
let allInRange = true;
for (let i = 0; i < 1000; i++) {
const value = getRandomInt(5, 10);
if (value < 5 || value > 10) {
allInRange = false;
}
}
console.log(allInRange);
const colors = ["red", "green", "blue"];
const pick = colors[Math.floor(Math.random() * colors.length)];
console.log(colors.includes(pick)); How It Works Range size. max - min + 1 is how many whole numbers are possible. For 5 to 10 that is 6.Scale and floor. Math.floor(Math.random() * 6) gives 0 to 5.Add min. Adding 5 moves the range to 5 to 10.Test. After 1000 tries every value stayed in range, so allInRange prints true.Random item. Use colors.length as the size to pick a random index, then read that item.Compatibility
Browser & Runtime Support Math.random is part of the first JavaScript standard (ES1 ) and is Baseline Widely available in every browser and in Node.js.
✓ Baseline · ES1
Math.random Supported in Chrome, Firefox, Safari, Edge, Internet Explorer and Node.js for many years. No polyfill needed.
99% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support Math.random Excellent
Bottom line: Use Math.random anywhere. It works in all browsers.
Frequently Asked Questions What does Math.random() return? It returns a random decimal from 0 up to but not including 1, like 0.4821. It can be 0 but it is never exactly 1.
How do I get a random whole number? Multiply and use Math.floor(). For 0 to 9 use Math.floor(Math.random() * 10). For 1 to 6 use Math.floor(Math.random() * 6) + 1.
How do I get a random number between min and max? Use Math.floor(Math.random() * (max - min + 1)) + min. This includes both min and max.
How do I pick a random item from an array? Use array[Math.floor(Math.random() * array.length)]. The index is always valid.
Is Math.random() safe for passwords? No. It is not secure. Use crypto.getRandomValues() when you need random values for passwords, tokens or keys.
Can I set a seed for Math.random()? No. JavaScript does not let you seed Math.random(), so you cannot repeat the same sequence. Use a small library if you need seeded random numbers.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion