Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

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.

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

Java

  1. Java List Interface: Overview, Implementations, and Key Methods
  2. Java TreeMap: Master Sorted Maps with Practical Examples
  3. Mastering Java ConcurrentHashMap: Operations, Examples, and Best Practices
  4. Java HashSet Class: Complete Guide to Operations, Methods, and Set Theory
  5. Mastering Java TreeSet: Operations, Methods, and Practical Examples
  6. Mastering Java Collection Algorithms: Sorting, Shuffling, and More
  7. Generating Random Numbers in VHDL: From Uniform to OSVVM
  8. Creating an Array of Objects in Java: A Step‑by‑Step Guide
  9. Java Numbers Class – Wrapper Classes & Inheritance Explained
  10. Java 8 Streams: A Declarative Approach to Data Processing