Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

util.Random: practical reference

util.Random: practical reference: Random generates a pseudorandom sequence with a 48-bit linear algorithm; the same seed and identical call sequence produce the same values.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

java.util.Random

Random generates a pseudorandom sequence with a 48-bit linear algorithm; the same seed and identical call sequence produce the same values.

Use an explicit seed for repeatable simulations and tests when the specified Random sequence must remain portable across Java implementations.

Random is not cryptographically secure, and sharing one instance across threads, although safe, can create contention.

nextInt(bound) requires a positive bound and throws IllegalArgumentException for zero or a negative value; use SecureRandom for secrets.

The example exercises the documented element in a small deterministic program and compares exact standard output.

Examples

public class Main {
    public static void main(String[] args) throws Exception {
        var random = new java.util.Random(123);
        System.out.println(random.nextInt(100));
    }
}

Result

82

The example exercises the documented element in a small deterministic program and compares exact standard output.

Sources