Utilities: new Random(seed) produces the same sequence every run

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.

Code
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));
}
Output
first=30 second=30
first=63 second=63
first=48 second=48
Advertisement
More in JAVA

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

© Java Coding Hub · About · Contact · Privacy · Terms