Random Numbers in Java: From Chaos to Order
\n\nHello, future Java developer! If you've ever written games, simulations, tests, or just wanted to add an element of surprise to your program, you've probably encountered the need to get a random integer in a given range. It seems simple, right? But in practice, beginners often ask: "Why am I getting numbers from 0 to 100, and not from 50 to 100?" or "Why is my code throwing an error?".
\n\nToday, we'll cover this topic once and for all. You'll learn how to generate a random number in Java in any range using three main approaches. Spoiler: one of them is the best choice for 90% of cases. Let's go!
\n\nWhy is this important?
\nGenerating random numbers is a basic but critically important task. It's essential for:
\n- \n
- Creating test data (e.g., user IDs). \n
- Implementing game mechanics (item drops, level generation). \n
- Modeling random processes (queues, traffic). \n
- Selecting a random element from a list. \n
By mastering this topic, you'll stop fearing the "magic" in your code and be able to confidently control randomness.
\n\nMethod #1: The Classic — java.util.Random
\nThis is the grandfather of all generators in Java. The java.util.Random class has existed since the earliest versions and is still actively used. Its main advantage is versatility.
How to get a random int from 0 to N?
\nThe nextInt(int bound) method returns a random number from 0 (inclusive) to bound (exclusive). So if you write nextInt(10), you'll get a number from 0 to 9.
import java.util.Random;\n\npublic class RandomExample {\n public static void main(String[] args) {\n Random random = new Random();\n int number = random.nextInt(10); // 0..9\n System.out.println("Random number: " + number);\n }\n}\n\nHow to do it from 5 to 15?
\nHere lies the main secret. To get a number in an arbitrary range [min, max], you need to use the formula:
min + random.nextInt(max - min + 1)
Let's break it down with an example: range from 5 to 15.
\n- \n
max - min + 1= 15 - 5 + 1 = 11. SonextInt(11)will give numbers from 0 to 10. \n - Add
min(5): we get the range 5..15. \n
Check it: if nextInt(11) returns 0, then 0+5 = 5. If it returns 10, then 10+5 = 15. Perfect!
Random random = new Random();\nint min = 5;\nint max = 15;\nint randomNumber = min + random.nextInt(max - min + 1);\nSystem.out.println("Random number from 5 to 15: " + randomNumber);\n\nImportant: Don't forget the +1! Without it, the maximum value will be 1 less.
Method #2: Modern Approach — ThreadLocalRandom
\nStarting with Java 7, the ThreadLocalRandom class was introduced. It is specifically designed for multithreaded applications (but works great in single-threaded ones too). The main advantage is that it's faster than Random and doesn't require creating a separate object.
Usage in single-threaded code
\nIt has a convenient method nextInt(int origin, int bound) that directly takes the range boundaries! Note: origin is inclusive, bound is exclusive. If you need to include the upper bound, just add +1.
import java.util.concurrent.ThreadLocalRandom;\n\npublic class ThreadLocalRandomExample {\n public static void main(String[] args) {\n // Number from 5 to 15 (inclusive)\n int randomNumber = ThreadLocalRandom.current().nextInt(5, 16);\n System.out.println("Random number: " + randomNumber);\n }\n}\n\nSee how simple it is? No formulas,