Random is deterministic once seeded: two Random instances constructed with the same seed produce exactly the same sequence of values, which is essential for reproducible tests and demos.
Random first = new Random(42);
Random second = new Random(42);
for (int i = 0; i < 3; i++) {
System.out.println("first=" + first.nextInt(100) + " second=" + second.nextInt(100));
}
first=30 second=30
first=63 second=63
first=48 second=48
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27