You will be able to generate pseudo-random numbers in JavaScript with Math.random() and convert them into useful ranges such as integers, dice rolls, and random array choices.
What it is
Math.random() is a built-in JavaScript function that returns a pseudo-random floating-point number greater than or equal to 0 and less than 1. It is not truly random; it is produced by an algorithm that looks random enough for many everyday tasks. Related terms include Math.floor(), Math.ceil(), Math.round(), and crypto.getRandomValues() for stronger randomness.
Why it matters
- Simulate chance-based behavior, such as dice rolls, coin flips, or loot drops.
- Choose a random item from an array for games, quizzes, or sample data.
- Create varied UI effects, such as random colors, positions, or placeholder values.
- Generate test data quickly during development.
- Build simple games and interactive demos without external libraries.
Syntax or steps
The smallest useful pattern is:
Math.random();
This gives a number like 0.734128. To get a random integer from 0 to 99, multiply by 100 and round down:
Math.floor(Math.random() * 100);
For a range from min to max inclusive, use:
Math.floor(Math.random() * (max - min + 1)) + min;
Example
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function randomChoice(items) {
return items[randomInt(0, items.length - 1)];
}
const dice = randomInt(1, 6);
const colors = ["red", "green", "blue"];
const chosenColor = randomChoice(colors);
console.log("Dice:", dice);
console.log("Color:", chosenColor);
Part by part:
Math.random()produces a number from 0 up to but not including 1.(max - min + 1)creates the number of possible integer values in the range.Math.floor()removes the decimal part, giving an integer.+ minshifts the result so it starts at the desired minimum.randomChoice()uses the random integer as an array index.
Common mistakes
- Forgetting that Math.random() never returns 1. If you need 1 to 100, use Math.floor(Math.random() * 100) + 1, not Math.ceil(Math.random() * 100) unless you understand the edge case.
- Using Math.round() for ranges. It can make the first and last values less likely because rounding boundaries are uneven.
- Assuming Math.random() is secure. Do not use it for passwords, tokens, or gambling systems that need cryptographic randomness.
- Calling Math.random() repeatedly without storing the result when you need the same random value for multiple calculations.
When to use it
Use Math.random() for ordinary non-security randomness. Use crypto.getRandomValues() when unpredictability matters.
| Need | Use | Why |
|---|---|---|
| Game dice, random color, sample data | Math.random() | Simple, built-in, fast. |
| Security tokens, passwords, fair gambling | crypto.getRandomValues() | Stronger randomness and less predictable. |
| Reproducible tests or simulations | A seeded pseudo-random generator | Math.random() cannot be seeded in standard JavaScript. |
Practice
Guided exercise: Write a function called randomCoin() that returns either "heads" or "tails". Hint: use Math.random() and compare it with 0.5.
function randomCoin() {
return Math.random() < 0.5 ? "heads" : "tails";
}
Challenge: Create a function randomLetter() that returns a random lowercase letter from "a" to "z". Expected output: one character such as "m". Hint: use a string of letters and randomChoice(), or use character codes.
Quick check
Question: What does Math.floor(Math.random() * 6) + 1 produce?
Answer: A random integer from 1 to 6 inclusive, like a six-sided dice roll.
Summary
Math.random() gives a floating-point number from 0 up to but not including 1. By multiplying, flooring, and shifting, you can turn it into practical random integers and choices. Use it for everyday randomness, but choose stronger tools when security matters.