Understanding Math.random() in JavaScript — The Most Misunderstood Function
When I first started learning JavaScript, Math.random() felt like magic.
A single function that creates randomness inside your code?
Sounds simple — but the details matter.
This article breaks down exactly what Math.random() does, how it works under the hood, and how you can use it with full confidence.
🔍 What Math.random() Actually Does
Math.random() is a JavaScript method that returns a pseudo-random number between:
0 (inclusive) → 1 (exclusive)
Meaning:
- It can return
0 - It will never return
1 - It always returns a floating-point number like:
0.2917,0.0523,0.9994, etc.
This small detail (“exclusive of 1”) becomes very important when generating ranges.
🎯 Why the “exclusive of 1” part matters
If you call:
Math.random()You might get numbers like:
0.107
0.8349
0.0032But never:
1This is crucial when you’re generating whole numbers or selecting random array elements.
📌 Example: Getting a Random Number Between 0 and 10
Wrong approach (most beginners do this):
Math.random() * 10;This gives numbers between 0 and 9.9999…, never 10.
Correct approach if you want whole numbers:
Math.floor(Math.random() * 11);Now you get integers between 0 and 10 (inclusive).
🎲 Generating Random Integers In Any Range
Here’s the formula every developer must memorize:
Math.floor(Math.random() * (max - min + 1)) + min;Example: random number between 50 and 100:
Math.floor(Math.random() * (100 - 50 + 1)) + 50;Simple. Clean. Reliable.
🧠 Wait — is it truly random?
No.
JavaScript uses a pseudo-random number generator (PRNG).
Get dontknowJS’s stories in your inbox
Join Medium for free to get updates from this writer.
This means:
- Numbers appear random
- But they follow a deterministic algorithm
- Perfect for games, animations, calculations
- Not suitable for cryptography
For real randomness, JavaScript provides another method:
crypto.getRandomValues()(But that’s a topic for another article.)
🌟 Practical Uses of Math.random()
Here are everyday things developers use it for:
- Shuffle items
- Select a random element from an array
- Decide a 50/50 outcome
- Generate random colors
- Create random password-like strings
- Simple games (dice, cards, etc.)
Example: random array element —
const items = ["red", "blue", "green", "yellow"];
const randomItem = items[Math.floor(Math.random() * items.length)];console.log(randomItem);💡 Common Mistakes (and How to Avoid Them)
❌ Expecting whole numbers
→ Fix: wrap with Math.floor() or Math.round()
❌ Forgetting that 1 is never included
→ Fix: adjust your ranges accordingly
❌ Using it for secure features
→ Fix: use crypto.getRandomValues() instead
✨ Final Thoughts
Math.random() is one of the first “power tools” you learn in JavaScript.
But using it correctly can be the difference between code that works perfectly…
and code with unpredictable bugs.
You now understand:
- what the function returns
- how to generate ranges
- how to avoid common mistakes
- where it should (and shouldn’t) be used
A tiny function — but a huge step in leveling up your JavaScript skills.
📝 Practice Questions
🔹 Easy
- What is the output range of
Math.random()?
a) 0 to 1 (inclusive)
b) 0 (inclusive) to 1 (exclusive)
c) 0 to 1 (exclusive)
d) 1 to 2 (exclusive) - What type of value does
Math.random()return?
a) Integer
b) Boolean
c) Floating-point number
d) String - Which of the following can
Math.random()never return?
a) 0
b) 0.999999
c) 0.5
d) 1
🔹 Medium
- You want a random integer between 1 and 5 (both included).
Which formula is correct?
a)Math.random() * 5 + 1
b)Math.floor(Math.random() * 5) + 1
c)Math.floor(Math.random() * (5 - 1)) + 1
d)Math.ceil(Math.random() * 5) - What will this code output?
Math.floor(Math.random() * 3);
a) Only 0
b) 0, 1, or 2
c) 1, 2, or 3
d) 0, 1, 2, or 3
3. How do you get a random number between 10 and 20 (inclusive)?
a) Math.random() * 10 + 10
b) Math.floor(Math.random() * (20 - 10 + 1)) + 10
c) Math.floor(Math.random() * 20) + 10
d) Math.ceil(Math.random() * 10) + 10
🔹 Hard
- You need a random EVEN number between 20 and 40 (inclusive).
What is the correct approach?
a) Generate a number between 20–40, then divide by 2
b) Generate a number between 10–20, multiply by 2
c) Generate a number between 0–20, multiply by 2
d) UseMath.random()twice - What is a major limitation of using
Math.random()for secure applications like OTP generation?
a) It's too slow
b) It’s predictable (pseudo-random)
c) It returns only whole numbers
d) It returns only decimals - What will the following code return.
const colors = ["red", "green", "blue"];
const index = Math.floor(Math.random() * colors.length);
console.log(colors[index]);a) Always “red”
b) One random color from the array
c) Error: invalid index
d) Only “blue” or “green”









