In Java, Math.random() returns a pseudo-random double that is greater than or equal to 0.0 and less than1.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
Call
What it does
Math.random()
A double with 0.0 <= value < 1.0.
(int) (Math.random() * 6) + 1
Dice roll: 1, 2, 3, 4, 5, or 6.
(int) (Math.random() * arr.length)
Random index of an array.
Math.random() < 0.25
true 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
Parameter
Description
None
random() takes no parameters.
Return Value
Return Value
Description
double
A 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:
Rounds to nearest; it makes the first and last values half as likely, so do not use it for dice.
java.util.Random
Has 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);
}
}
Output
Samples taken: 1000
Samples in [0.0, 1.0): 1000
Draws that reached 1.0: 0
How It Works
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.
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.
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);
}
}
Output
r = 0.0 -> roll 1
r = 0.5 -> roll 4
r = 0.999 -> roll 6
1000 rolls all between 1 and 6? true
1000 indexes all valid? true
1000 values all between 10 and 20? true
How It Works
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.
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.
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");
}
}
Output
Largest value of (int) Math.random() * 6: 0
Correct version stays in 0..5? true
Chance 100%: 1000 of 1000
Chance 0%: 0 of 1000
How It Works
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.
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.
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.