Java Math random() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Math

Definition

In Java, Math.random() returns a pseudo-random double that is greater than or equal to 0.0 and less than 1.0. It takes no arguments and gives a different value on nearly every call.

Multiply it to stretch the range and cast to int for whole numbers: (int) (Math.random() * 6) + 1 is a dice roll from 1 to 6.

A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.random(...). No import is needed, because classes in java.lang are always visible.

Syntax

Call it with no arguments. Scale and shift the result to get the range you need.

Java
double r = Math.random();                       // 0.0 <= r < 1.0
int roll = (int) (Math.random() * 6) + 1;       // 1..6
int index = (int) (Math.random() * names.length);   // valid index
int pick = min + (int) (Math.random() * (max - min + 1));
boolean heads = Math.random() < 0.5;            // coin flip
CallWhat it does
Math.random()A double with 0.0 <= value < 1.0.
(int) (Math.random() * 6) + 1Dice roll: 1, 2, 3, 4, 5, or 6.
(int) (Math.random() * arr.length)Random index of an array.
Math.random() &lt; 0.25true with a 25 percent chance.

Multiply before casting: (int) Math.random() * 6 casts first and is always 0. Put parentheses around the product.

Library declarations
Java
// java.lang.Math
public static double random()

Parameters

ParameterDescription
Nonerandom() takes no parameters.

Return Value

Return ValueDescription
doubleA pseudo-random value from 0.0 (inclusive) up to 1.0 (exclusive). The value 1.0 is never returned.

Compare with Similar Methods

Ways to turn the random double into whole numbers:

MethodHow it differs
floor(a)Rounds down to a whole double; same as an (int) cast for positive values.
ceil(a)Rounds up; it can return 0 on an exact 0.0 draw, so prefer floor plus 1.
round(a)Rounds to nearest; it makes the first and last values half as likely, so do not use it for dice.
java.util.RandomHas nextInt(bound), seeds for repeatable numbers, and more types.

Java Examples

Example 1 — Check That Every Value Is in 0.0 to 1.0

Draw 1000 numbers and verify the range. The printed lines are the same on every run.

Java
public class MathRandomRange {
    public static void main(String[] args) {
        int samples = 1000;
        int inRange = 0;
        int reachedOne = 0;
        for (int i = 0; i < samples; i++) {
            double r = Math.random();
            if (r >= 0.0 && r < 1.0) {
                inRange++;
            }
            if (r >= 1.0) {
                reachedOne++;
            }
        }
        System.out.println("Samples taken: " + samples);
        System.out.println("Samples in [0.0, 1.0): " + inRange);
        System.out.println("Draws that reached 1.0: " + reachedOne);
    }
}

How It Works

  1. Never raw values. Each call to Math.random() returns a new number, so the example counts range checks instead of printing the numbers; the output below never changes.
  2. A half-open range. 0.0 is allowed, 1.0 is not, which is why the test uses >= for the lower and < for the upper limit, and all 1000 pass.
  3. Upper limit is exclusive. A second counter looks for draws of 1.0 or more; it stays at 0, because the generator can come arbitrarily close to 1.0 but never reaches it.

Example 2 — Dice Rolls, Random Picks, and a Range Formula

See the formulas with fixed numbers in place of Math.random(), then run them for real.

Java
public class MathRandomDice {
    public static void main(String[] args) {
        System.out.println("r = 0.0    -> roll " + ((int) (0.0 * 6) + 1));
        System.out.println("r = 0.5    -> roll " + ((int) (0.5 * 6) + 1));
        System.out.println("r = 0.999  -> roll " + ((int) (0.999 * 6) + 1));
        String[] names = {"Ann", "Ben", "Cy", "Dee"};
        boolean rollsOk = true;
        boolean indexOk = true;
        boolean rangeOk = true;
        for (int i = 0; i < 1000; i++) {
            int roll = (int) (Math.random() * 6) + 1;
            int index = (int) (Math.random() * names.length);
            int value = 10 + (int) (Math.random() * 11);
            rollsOk = rollsOk && roll >= 1 && roll <= 6;
            indexOk = indexOk && index >= 0 && index < names.length;
            rangeOk = rangeOk && value >= 10 && value <= 20;
        }
        System.out.println("1000 rolls all between 1 and 6? " + rollsOk);
        System.out.println("1000 indexes all valid? " + indexOk);
        System.out.println("1000 values all between 10 and 20? " + rangeOk);
    }
}

How It Works

  1. Stretch, cut, shift. Times 6 turns 0.0 to 0.999 into 0.0 to 5.994; the (int) cast cuts the decimals (0 to 5), and + 1 shifts to 1 to 6. The fixed inputs 0.0, 0.5, and 0.999 give 1, 4, and 6.
  2. General range. For min to max the factor is max - min + 1, which is 11 for 10 to 20; the largest cut result is 10, so the largest value is 10 + 10 = 20.
  3. Safe indexes. Multiplying by names.length (4) gives 0.0 up to just below 4.0, so the cast yields 0, 1, 2, or 3, always a valid index, and the flags stay true in every run.

Example 3 — The (int) Math.random() * 6 Trap and Percentage Chances

One misplaced cast makes a dice roll constant. Also see the 100 percent and 0 percent chances.

Java
public class MathRandomPitfall {
    public static void main(String[] args) {
        int wrongMax = 0;
        int rightMin = 99;
        int rightMax = -1;
        int always = 0;
        int never = 0;
        for (int i = 0; i < 1000; i++) {
            int wrong = (int) Math.random() * 6;
            int right = (int) (Math.random() * 6);
            wrongMax = Math.max(wrongMax, wrong);
            rightMin = Math.min(rightMin, right);
            rightMax = Math.max(rightMax, right);
            if (Math.random() < 1.0) {
                always++;
            }
            if (Math.random() < 0.0) {
                never++;
            }
        }
        System.out.println("Largest value of (int) Math.random() * 6: " + wrongMax);
        System.out.println("Correct version stays in 0..5? " + (rightMin >= 0 && rightMax <= 5));
        System.out.println("Chance 100%: " + always + " of 1000");
        System.out.println("Chance 0%: " + never + " of 1000");
    }
}

How It Works

  1. The cast binds first. In (int) Math.random() * 6 the cast applies to Math.random() alone; any value from 0.0 to 0.999... becomes 0, and 0 * 6 is 0, so the largest result over 1000 tries is 0.
  2. Parentheses fix it. (int) (Math.random() * 6) multiplies first and casts the product, giving 0 to 5, so both rightMin and rightMax stay inside that range.
  3. Boundary chances. Every draw is below 1.0 and no draw is below 0.0, so Math.random() < 1.0 (a 100 percent chance) always hits and < 0.0 never does; a 25 percent chance uses < 0.25.

Frequently Asked Questions

A pseudo-random double that is at least 0.0 and less than 1.0. The value 1.0 is never returned.
Use min + (int) (Math.random() * (max - min + 1)). For a dice roll from 1 to 6 this is (int) (Math.random() * 6) + 1.
The cast applies to Math.random() before the multiplication. Math.random() is always below 1.0, so the cast gives 0, and 0 * 6 is 0. Write (int) (Math.random() * 6) with parentheses.
No, so the numbers cannot be reproduced. Create a java.util.Random with a seed when you need the same sequence on every run, for example in tests.
No. It is not designed to be unpredictable. Use java.security.SecureRandom for security purposes, and ThreadLocalRandom for heavy multi-threaded use.

Next: rint()

Round a double to the nearest whole number; halves go to the even neighbour.

rint() 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.

13 people found this page helpful