Java has three main ways to create random numbers: Math.random(), the Random class, and ThreadLocalRandom for multi-threaded programs

The simplest approach is Math.random(), which returns a decimal between 0.0 and 1.0. If you need an integer instead, multiply by the range you want and cast to int — for example, (int)(Math.random() * 100) gives you 0 to 99. This works for quick, single-use cases but becomes cumbersome if you need many random numbers or specific ranges.

For more control, use the Random class from java.util. Create an instance with Random rand = new Random();, then call methods like rand.nextInt(100) for an integer from 0 to 99, or rand.nextDouble() for a decimal. This approach is cleaner when you generate multiple random values in the same program.

If your program uses multiple threads, use ThreadLocalRandom instead. Call ThreadLocalRandom.current().nextInt(100) directly — no need to create an instance. ThreadLocalRandom avoids the contention that slows down Random when threads compete for the same object.

Key Takeaways

  • Math.random() returns a decimal between 0.0 and 1.0; multiply and cast to int if you need a whole number.
  • The Random class lets you call nextInt(), nextDouble(), nextLong(), and other methods on a reusable object.
  • ThreadLocalRandom performs better in multi-threaded programs because each thread gets its own random number generator.
  • All three methods use a seed internally; set it with new Random(seed) if you need reproducible results for testing.

Using Math.random() for simple cases

Math.random() is the shortest path when you need one or two random values. The method lives in the java.lang package, so you do not need an import. It always returns a double between 0.0 (inclusive) and 1.0 (exclusive).

To get an integer, multiply by your upper bound and cast to int. The expression (int)(Math.random() * 100) produces 0 through 99. If you want 1 through 100 instead, use (int)(Math.random() * 100) + 1. The cast truncates the decimal, so 99.9 becomes 99.

The downside: calling Math.random() multiple times in a loop is less efficient than creating a Random object once and reusing it. For a single random value in a method, Math.random() is fine. For a loop that runs 100 times, the Random class is better.

Creating and using a Random object

The Random class gives you more methods and better performance when you need many random numbers. Import it with import java.util.Random;, then create an instance: Random rand = new Random();

Call nextInt(bound) to get an integer from 0 to bound minus 1. So rand.nextInt(100) returns 0 through 99. Call nextDouble() for a decimal between 0.0 and 1.0, just like Math.random(). Other methods include nextLong(), nextBoolean(), and nextFloat().

Create the Random object once at the class level or method level, then reuse it. Creating a new Random object each time you need a number defeats the purpose — the constructor seeds the generator, which is slower than calling nextInt() on an existing object.

Choosing ThreadLocalRandom for concurrent programs

When multiple threads call nextInt() on the same Random object, they contend for a lock, which slows them down. ThreadLocalRandom solves this by giving each thread its own generator. You do not create an instance; instead, call ThreadLocalRandom.current().nextInt(100) directly.

ThreadLocalRandom is part of java.util.concurrent, so import it with import java.util.concurrent.ThreadLocalRandom; The current() method returns the generator for the thread that called it. If thread A and thread B both call current(), they get different objects, so no lock is needed.

Use ThreadLocalRandom in any program with multiple threads — a web server handling requests, a thread pool processing tasks, or a parallel stream. For single-threaded programs, Random is sufficient and slightly simpler.

Setting a seed for reproducible results

By default, Random seeds itself with the system clock, so each run produces different numbers. For testing, you often want the same sequence every time. Pass a seed to the Random constructor: Random rand = new Random(42); Every run with seed 42 produces the same sequence.

Seeds are long integers. Any seed value works; 42 is just a convention. If you hardcode a seed in production code, your program becomes predictable, which is bad for security (random tokens, shuffling, games). Use seeds only in unit tests or when you need to replay a scenario for debugging.

ThreadLocalRandom does not accept a seed in the constructor, but you can set one with ThreadLocalRandom.current().setSeed(42) if needed. This is rare — most uses of ThreadLocalRandom are in production, not testing.

Generating random numbers in a specific range

To get a random integer between two bounds, use rand.nextInt(max - min) + min. For example, to roll a six-sided die (1 through 6), write rand.nextInt(6) + 1. The nextInt(6) part returns 0 through 5, and adding 1 shifts it to 1 through 6.

For decimals in a range, multiply and add: rand.nextDouble() * (max - min) + min. To get a random double between 5.0 and 10.0, use rand.nextDouble() * 5.0 + 5.0. The nextDouble() returns 0.0 to 1.0, multiply by the width (5.0), then add the floor (5.0).

Java 17 and later added nextInt(origin, bound) to Random, which is cleaner: rand.nextInt(1, 7) returns 1 through 6 directly. Check your Java version — if you are on 17 or later, use this method instead of the arithmetic approach.

Common mistakes and how to avoid them

Creating a new Random object in a loop wastes CPU on seeding. Write the object creation outside the loop, then call nextInt() inside. If you see for (int i = 0; i < 100; i++) { Random rand = new Random(); }, move the Random declaration before the loop.

Forgetting to add 1 when you want a range starting at 1 is easy. nextInt(6) gives 0 through 5, not 1 through 6. If you are rolling dice or picking from a list that starts at index 1, add 1 to the result.

Mixing Math.random() and Random in the same program is not wrong, but it is confusing. Pick one approach and stick with it. If you need just one random value, Math.random() is fine. If you need more than one, use Random or ThreadLocalRandom.

Frequently Asked Questions

What is the difference between Math.random() and Random.nextDouble()?

Both return a decimal between 0.0 and 1.0, but Math.random() creates a new Random object behind the scenes each time you call it, while nextDouble() reuses an existing object. For a single call, Math.random() is simpler. For multiple calls, Random is faster.

Can I use Random to shuffle a list?

Yes. The Collections class has a shuffle() method that takes a list and a Random object: Collections.shuffle(myList, rand); This randomizes the order of elements. If you do not pass a Random object, shuffle() creates one internally.

Why does nextInt(100) return 0 to 99 instead of 1 to 100?

This matches how arrays work in Java — indices start at 0. If you have an array of 100 elements, you access them with indices 0 through 99. Returning 0 to 99 makes it natural to pick a random element with myArray[rand.nextInt(100)] Add 1 only if your use case requires it.

Is Random thread-safe?

Random is thread-safe in the sense that it will not crash if multiple threads call it at once, but the calls are serialized by a lock, which is slow. ThreadLocalRandom avoids the lock by giving each thread its own generator, so it is faster in multi-threaded code.

How do I generate a random number between two decimals?

Use rand.nextDouble() * (max - min) + min. For a random double between 5.5 and 10.5, write rand.nextDouble() * 5.0 + 5.5. Java 17 and later also support nextDouble(origin, bound) for a cleaner syntax.