0Pricing
Java Academy · Lesson

Generating Random Numbers

Random and ThreadLocalRandom.

Generating Random Numbers is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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);
    }
}

Scaling Math.random

Multiply and cast to get an integer range. (int)(Math.random() * 6) yields 0 through 5.

public class Main {
    public static void main(String[] args) {
        int roll = (int)(Math.random() * 6) + 1;
        System.out.println("Dice: " + roll);
    }
}

The Random Class

The java.util.Random class is more flexible. Create an instance, then call methods like nextInt, nextDouble, and nextBoolean.

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Random rng = new Random();
        System.out.println(rng.nextBoolean());
        System.out.println(rng.nextDouble());
    }
}

nextInt with a Bound

nextInt(bound) returns an int from 0 (inclusive) to bound (exclusive). nextInt(6) gives 0 through 5.

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Random rng = new Random();
        int value = rng.nextInt(100);
        System.out.println("0 to 99: " + value);
    }
}

Seeding for Reproducibility

Passing a seed to the constructor makes the sequence deterministic: the same seed always produces the same numbers. This is invaluable for testing.

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Random rng = new Random(42);
        System.out.println(rng.nextInt(1000));
        System.out.println(rng.nextInt(1000));
    }
}

Same Seed, Same Sequence

Two Random objects built with the same seed emit identical sequences. Useful when a bug must be reproduced exactly.

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Random a = new Random(7);
        Random b = new Random(7);
        System.out.println(a.nextInt(50) == b.nextInt(50));
    }
}

ThreadLocalRandom

In multithreaded programs a single shared Random becomes a bottleneck. ThreadLocalRandom.current() gives each thread its own generator with no contention.

import java.util.concurrent.ThreadLocalRandom;

public class Main {
    public static void main(String[] args) {
        int value = ThreadLocalRandom.current().nextInt(1, 7);
        System.out.println("Dice: " + value);
    }
}

Never Share One Random Across Threads

Do not pass a single Random instance to many threads; it can produce poor distribution and contention. Prefer ThreadLocalRandom in concurrent code.

Doubles and Gaussians

nextDouble() gives [0,1), and nextGaussian() returns values from a normal distribution with mean 0 and standard deviation 1.

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Random rng = new Random(1);
        System.out.println(rng.nextDouble());
        System.out.println(rng.nextGaussian());
    }
}

Choosing the Right Tool

Use Math.random() for a quick double, Random when you want bounds or a seed, and ThreadLocalRandom in concurrent code. For security-sensitive randomness, use SecureRandom instead.

Quick Check

Test your understanding of randomness.

Recap

You learned to generate random numbers.

  • Math.random() gives a double in [0,1).
  • Random.nextInt(bound) gives 0 to bound-1.
  • A seed makes sequences reproducible.
  • ThreadLocalRandom is the concurrent-safe choice.

Frequently asked questions

Is the “Generating Random Numbers” lesson free?

Yes — the full text of “Generating Random Numbers” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Generating Random Numbers”?

Random and ThreadLocalRandom. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Generating Random Numbers” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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