How to Generate Random Numbers in JavaScript
Use Math.random() when you need random values for games, UI variations, test data, IDs, or quick utilities. The key is converting the decimal result into the exact range you need.
What you’ll build or solve
You’ll learn how to generate random numbers in JavaScript with Math.random(). You’ll also know how to create whole numbers, ranges, and safe min-to-max values.
Learn JavaScript on Mimo
When this approach works best
This approach is the right choice when approximate randomness is enough for UI, games, or non-secure utilities.
Common real-world scenarios include:
- Dice rolls
- Random quotes
- A/B UI variations
- Shuffle helpers
- Mock data
This is a bad idea for passwords, tokens, or security-sensitive randomness. Use the Web Crypto API instead.
Prerequisites
You only need:
- A JavaScript file or browser console
- Basic math knowledge
Step-by-step instructions
Step 1: Use Math.random() and scale the result
Math.random() returns a decimal from 0 up to, but not including, 1.
JavaScript
const randomDecimal = Math.random();
console.log(randomDecimal);
To generate a whole number from 0 to 9, multiply and floor it.
JavaScript
const randomNumber = Math.floor(Math.random() * 10);
console.log(randomNumber);
To generate a min-to-max range, use this formula.
JavaScript
function randomBetween(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
What to look for:
Math.random()returns decimals- Multiply to scale the range
- Use
Math.floor()for whole numbers - Add
minfor custom ranges - Not suitable for secure randomness
Examples you can copy
Dice roll
JavaScript
const dice = Math.floor(Math.random() * 6) + 1;
Random index
JavaScript
const index = Math.floor(Math.random() * items.length);
Random range
JavaScript
const age = randomBetween(18, 65);
Common mistakes and how to fix them
Mistake 1: Forgetting Math.floor()
What the reader might do:
JavaScript
const roll = Math.random() * 6;
Why it breaks: this returns decimals like 4.28.
Corrected approach:
JavaScript
const roll = Math.floor(Math.random() * 6);
Mistake 2: Missing the inclusive max adjustment
What the reader might do:
JavaScript
Math.floor(Math.random() * (max - min)) + min;
Why it breaks: the max value can never appear.
Corrected approach:
JavaScript
Math.floor(Math.random() * (max - min + 1)) + min;
Mistake 3: Using it for passwords or tokens
What the reader might do:
JavaScript
const token = Math.random().toString(36);
Why it breaks: Math.random() is not cryptographically secure.
Corrected approach:
Use the Web Crypto API for secure randomness.
Troubleshooting
If decimals appear, add Math.floor().
If the max value never shows, add + 1 inside the range math.
If the range starts too high, verify the + min part.
If security matters, avoid Math.random().
Quick recap
- Use
Math.random()for decimals - Multiply to scale the range
- Use
Math.floor()for integers - Add
minfor custom ranges - Avoid it for secure tokens
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot