Loading...

Java Random

Ask Java for a chance number in a small range and print it. In Dice.java, import java.util.Random, keep the public class Dice, build Random rng = new Random(42);, store int roll = rng.nextInt(6) + 1;, and print roll. Compile with javac Dice.java and run java Dice. With seed 42 the screen should show 3. A second demo prints two rolls from the same generator so the screen shows 3, then 4. Leave Summary for the next page — this lesson is chance numbers with a fixed seed for practice, not a course wrap-up.

Build one seeded roll

The dates page froze a calendar day. This page asks for a roll. Open Dice.java. Keep the public class name Dice. Delete leftover Stamp date demos if they bury the Random story, leftover Yard formatting sums, leftover naming files, and any keyboard Scanner. The program must not wait for typing.
Add the import at the top of the file:
import java.util.Random;
Inside Dice.main, build a generator with a seed so every run of this teaching file repeats the same first value:
Random rng = new Random(42);
int roll = rng.nextInt(6) + 1;
System.out.println(roll);
nextInt(6) alone would return 0 through 5. Adding 1 shifts the range to 1 through 6, like a six-sided die. The seed 42 is not magic — it only locks the sequence for this lesson so the quiz and the screen agree. You are not guessing a new number each time you read the page.

Dice one seeded roll prints 3

java
import java.util.Random;

public class Dice {
    public static void main(String[] args) {
        Random rng = new Random(42);
        int roll = rng.nextInt(6) + 1;
        System.out.println(roll);
    }
}

From the folder that holds Dice.java, run javac Dice.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Dice — not java Dice.java. Leave the .java ending off the run command. Keep one public class in the file so the name matches Dice.
The screen shows 3. Walk the run once. Java starts at Dice.main. The constructor stores seed 42. The first nextInt(6) call draws a value in 0..5, then + 1 makes 1..6. With this seed that first roll becomes 3. The print writes 3.
Say the beginner rule while you watch the screen: import Random, build with a seed when you need a repeatable demo, call nextInt(6) + 1 for a die-like range, print the roll. Do not invent a Summary chapter on this page. Summary waits for the next lesson. Do not drop the + 1 if you want 1 through 6.
If you still see 2026-03-15 from a leftover Stamp dates file, switch to the Random teaching file and run java Dice again. If javac complains that Random cannot be found, check the import line at the top of Dice.java.

A second roll from the same generator

One roll proves the range. A second call on the same Random object continues the sequence. Keep the same seed constructor. Print two rolls:
System.out.println(rng.nextInt(6) + 1);
System.out.println(rng.nextInt(6) + 1);
With seed 42, the first print is still 3. The second print is 4. Both values stay inside 1..6. The generator remembers where it is; you do not rebuild new Random(42) between those two lines if you want the next value in the same sequence.

Dice two seeded rolls print 3 then 4

java
import java.util.Random;

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

Compile with javac Dice.java and run java Dice again. The screen shows 3, then 4. Walk the run once. The same seed starts the sequence. The first nextInt(6) + 1 prints 3. The second call advances and prints 4. Nothing waits for the keyboard. Nothing stores a calendar day.
Keep the two demos as two teaching shapes. First prove one seeded roll. Then prove two rolls in order. Both shapes share the same import and the same seed. The second shape only adds another nextInt call.
If you rebuild new Random(42) before every print, each line starts the sequence again and you may see 3 twice. That is a different habit. This page keeps one generator for the two-roll demo so the second line advances.

Random is not a calendar and not Summary

Dates stored a fixed day you could print and split. Random draws chance numbers in a range. Summary will wrap the course path on the next page. Do not mix those three jobs in one beginner file today.
Without a seed, each run can show a different first roll. That is fine for games later. This lesson uses seed 42 so the teaching screen and the quiz share one answer. You still learn the same nextInt call either way.
When the import is missing, javac fails before any screen line appears. When you forget + 1, a 0 can appear, which is not a standard die face. Keep the import, keep the shift, and run java Dice until the one-roll demo shows 3 or the two-roll demo shows 3 then 4.
If leftover date demos still sit nearby, keep them as history. For this lesson run the Random teaching file so the screen shows chance numbers in range, not 2026-03-15. If javac complains about duplicate public classes, keep only Dice public.

Range, seed, and the + 1 habit

nextInt(6) means six possible starting values: 0, 1, 2, 3, 4, and 5. The expression nextInt(6) + 1 maps them to 1, 2, 3, 4, 5, and 6. Say that mapping out loud once while you look at the code. The bound argument is the count of starting values, not the highest face after the shift.
The seed locks which path through those faces you see first in this teaching file. Changing the seed changes the sequence. Leaving the seed out changes the sequence across runs. For graded practice on this page, keep 42 so the first face stays 3.
Do not treat Random as a replacement for LocalDate. Do not treat a roll as a formatting indent rule. Each earlier page kept its own job. This page keeps chance numbers only.

Before Summary

You can import Random, build new Random(42), roll nextInt(6) + 1, see 3 on one run, and see 3 then 4 on the two-roll demo. Compile Dice.java and run java Dice for the kept demos. The range stayed 1..6; the seed kept the sequence stable for teaching. The next page is Summary, where you look back across the Java path.
Keep the seeded dice demos for a clear chance-number pair. Keep dates files off this page when you practice Random. Keep Summary files off this page until that lesson. Those three habits are the checkpoint before you move on. When the one-roll screen shows 3 or the two-roll screen shows 3 then 4, Random did its job for this lesson and you are ready for the next topic about Summary.
One last pass through the folder: one public Dice, import present, new Random(42), nextInt(6) + 1, no keyboard wait, no forced calendar types. Run javac Dice.java and java Dice once more on the demo you keep open. If the screen still shows 3 or 3 then 4, the Random shape is solid and the course can move to Summary on the next page. Prefer one clear chance habit per read so each pass teaches the same map.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top