Back to JavaScript Notes
Topic #339

Math Random

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.
  • + min shifts 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.

NeedUseWhy
Game dice, random color, sample dataMath.random()Simple, built-in, fast.
Security tokens, passwords, fair gamblingcrypto.getRandomValues()Stronger randomness and less predictable.
Reproducible tests or simulationsA seeded pseudo-random generatorMath.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.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Math Random – FAQs

Quick answers about learning Math Random in JavaScript.

This free note from Coding Now Tech Institute explains Math Random in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including Math Random, is 100% free with no signup required.
With focused practice, most students grasp Math Random in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now