JavaScript Math.random() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
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.

JavaScript
Math.random()
CallWhat 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
JavaScript
Math.random()

Parameters

None. Math.random() takes no arguments.

Return Value

Return ValueDescription
NumberA random decimal that is at least 0 and always less than 1.

JavaScript Examples

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.

JavaScript
const n = Math.random();
console.log(n >= 0); // true
console.log(n < 1); // true
console.log(typeof n); // number
Try It Yourself

How It Works

  1. New number each time. Math.random() returns something like 0.7318.... It is different on every call.
  2. Never negative. The value is always 0 or more.
  3. Never 1. The value is always smaller than 1.
  4. 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.

JavaScript
const dice = Math.floor(Math.random() * 6) + 1;
console.log("You rolled: " + dice);
Try It Yourself

How It Works

  1. Scale. Math.random() * 6 gives a decimal from 0 up to just below 6.
  2. Cut the decimal. Math.floor() turns it into 0, 1, 2, 3, 4 or 5.
  3. Shift by one. Adding 1 makes the range 1 to 6.
  4. 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.

JavaScript
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));
Try It Yourself

How It Works

  1. Range size. max - min + 1 is how many whole numbers are possible. For 5 to 10 that is 6.
  2. Scale and floor. Math.floor(Math.random() * 6) gives 0 to 5.
  3. Add min. Adding 5 moves the range to 5 to 10.
  4. Test. After 1000 tries every value stayed in range, so allInRange prints true.
  5. Random item. Use colors.length as the size to pick a random index, then read that item.

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

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.
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.
Use Math.floor(Math.random() * (max - min + 1)) + min. This includes both min and max.
Use array[Math.floor(Math.random() * array.length)]. The index is always valid.
No. It is not secure. Use crypto.getRandomValues() when you need random values for passwords, tokens or keys.
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.

Next: round()

Learn how to round to the nearest integer.

round() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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