Generate Random Numbers in Java: Practical Guide with Random and Math.random
In this concise tutorial you’ll learn how to produce random values in Java using the two most common approaches: java.util.Random and Math.random(). Both are part of the JDK, but each serves slightly different use‑cases and offers unique features.
- java.util.Random – Generates numbers of any primitive type and can be seeded for reproducibility.
- Math.random() – Provides a quick way to get a double in the range [0.0, 1.0) without creating an object.
Random Number Generation with java.util.Random
The Random class can produce int, long, float, double, and boolean values. Instantiate it once and reuse the same object to avoid performance overhead.
Below is a complete example that outputs ten random integers between 0 and 99 (inclusive). If you need the upper bound to be inclusive, add one to the argument.
import java.util.Random;
public class RandomNumbers {
public static void main(String[] args) {
Random rng = new Random();
for (int i = 0; i < 10; i++) {
int randomNumber = rng.nextInt(100); // 0‑99
System.out.println("Random No: " + randomNumber);
}
}
}
Typical Output:
Random No: 17 Random No: 57 Random No: 73 Random No: 48 Random No: 68 Random No: 86 Random No: 34 Random No: 97 Random No: 73 Random No: 18
Because Random is thread‑safe for most uses, you can share the same instance across multiple threads if needed. For reproducible sequences, pass a seed: new Random(42).
Random Number Generation with Math.random()
When you only need a quick double in the range [0.0, 1.0), Math.random() is convenient. It internally delegates to a single Random instance, so it’s efficient for small workloads.
Example: generate ten double values.
public class DemoRandom {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(Math.random());
}
}
}
Typical Output:
0.46518450373334297 0.14859851177803485 0.5628391820492477 0.6323378498048606 0.1740198445692248 0.9140544122258946 0.9167350036262347 0.49251219841030147 0.7426056725722353
To generate integers or doubles in a different range, combine Math.random() with arithmetic. For example, to get a random int between 1 and 10 inclusive: (int)(Math.random() * 10) + 1.
Key Takeaways
- Use
java.util.Randomwhen you need a variety of primitive types or deterministic sequences via seeding. - Use
Math.random()for quick, one‑off doubles in the [0.0, 1.0) interval. - Always reuse the same
Randominstance to avoid repeated seeding overhead. - For thread‑safe, high‑performance randomness in concurrent environments, consider
ThreadLocalRandom(Java 7+).
Java
- Java List Interface: Overview, Implementations, and Key Methods
- Java TreeMap: Master Sorted Maps with Practical Examples
- Mastering Java ConcurrentHashMap: Operations, Examples, and Best Practices
- Java HashSet Class: Complete Guide to Operations, Methods, and Set Theory
- Mastering Java TreeSet: Operations, Methods, and Practical Examples
- Mastering Java Collection Algorithms: Sorting, Shuffling, and More
- Generating Random Numbers in VHDL: From Uniform to OSVVM
- Creating an Array of Objects in Java: A Step‑by‑Step Guide
- Java Numbers Class – Wrapper Classes & Inheritance Explained
- Java 8 Streams: A Declarative Approach to Data Processing