0Pricing
Java Academy · Lesson

Generating Random Numbers

Random and ThreadLocalRandom.

Generating Random Numbers

Randomness powers games, simulations, and sampling. Java offers Math.random(), the Random class, and ThreadLocalRandom for concurrent code.

Math.random Basics

Math.random() returns a double in the range [0.0, 1.0): zero inclusive, one exclusive.

public class Main {
    public static void main(String[] args) {
        double r = Math.random();
        System.out.println("Value between 0 and 1: " + r);
    }
}

All lessons in this course

  1. The Math Class
  2. Generating Random Numbers
  3. Random Ranges and Shuffling
  4. Math Rounding and Limits
← Back to Java Academy