How to Generate Random Numbers in Java
The Challenge
Generate random numbers for games, simulations, and testing!
Solutions
Method 1: Using Math.random()
// Random double between 0.0 and 1.0
double random = Math.random();
System.out.println(random); // e.g., 0.7234567
// Random int between 0 and 9
int randomInt = (int)(Math.random() * 10);
System.out.println(randomInt); // e.g., 7
// Random int between 1 and 100
int randomRange = (int)(Math.random() * 100) + 1;
System.out.println(randomRange); // e.g., 42Method 2: Using Random Class
import java.util.Random;
Random rand = new Random();
// Random int
int randomInt = rand.nextInt();
System.out.println(randomInt);
// Random int between 0 and 99
int randomRange = rand.nextInt(100);
System.out.println(randomRange);
// Random int between 1 and 100
int randomBetween = rand.nextInt(100) + 1;
System.out.println(randomBetween);
// Random double
double randomDouble = rand.nextDouble();
System.out.println(randomDouble);
// Random boolean
boolean randomBool = rand.nextBoolean();
System.out.println(randomBool);Method 3: Using ThreadLocalRandom (Java 7+)
import java.util.concurrent.ThreadLocalRandom;
// Random int between min (inclusive) and max (exclusive)
int random = ThreadLocalRandom.current().nextInt(1, 101);
System.out.println(random); // Between 1 and 100Common Patterns
// Random number between min and max (inclusive)
public static int randomBetween(int min, int max) {
return (int)(Math.random() * (max - min + 1)) + min;
}
// Usage
System.out.println(randomBetween(10, 20)); // e.g., 15Did You Know?
Java's random numbers are "pseudorandom" - they're generated by an algorithm, not truly random. For cryptographic security, use SecureRandom!
Pro Tip
Use ThreadLocalRandom for better performance in multi-threaded applications:
// Faster in concurrent environments
int num = ThreadLocalRandom.current().nextInt(1, 101);Real-World Use Cases
- Games: Dice rolls, card shuffling, enemy spawning
- Simulations: Monte Carlo simulations
- Testing: Generate test data
- Security: Generate tokens, passwords (use SecureRandom)
›Short versionOpen if you only have a minute.
- Math.random() is in [0.0, 1.0). It never returns 1.
- (int) (Math.random() * 100) is 0 through 99. Inclusive 0 through 100 needs * 101.
- Random.nextInt(bound) is 0 inclusive through bound exclusive, and bound must be positive.
- A random draw may repeat. It is not a shuffle and it is not a unique id.
Random number in a range
Generate a value in a specific range with clear inclusive or exclusive boundaries.
Working approach
- For an inclusive int range, call nextInt(max - min + 1) + min.
- Check max >= min before subtracting. A negative bound throws.
- Use SecureRandom only when the value is a secret. Ordinary Random is the right tool for a dice roll.
Walk the sample
- Inclusive range 1 to 6
- The bound width is 6
- nextInt(6) returns 0 through 5
- Adding 1 returns 1 through 6
The trap
The upper bound of Math.random() and of nextInt is exclusive. Forgetting the + 1 drops the highest value the caller asked for.
static int inclusive(Random random, int min, int max) {
if (max < min) {
throw new IllegalArgumentException("max < min");
}
return random.nextInt(max - min + 1) + min;
}Why does the index recipe multiply by 101 to get a number from 0 through 100?
Math.random() * 100 lands in [0, 100) and the cast keeps 0 through 99. 100 is never produced.
Multiplying by 101 lands in [0, 101) and the cast keeps 0 through 100. nextInt(101) says the same thing without the double.
Time O(1), space O(1).
Easy to get wrong
- Using max - min as the bound when both ends are inclusive.
- Creating a new Random on every call when one instance should be reused.