Java Random Class: A Guide to Pseudo-Random Number Generation
🧠 What Will You Learn in This Guide?
In this guide, you will learn how to generate pseudorandom numbers of different types (int, double, boolean) using Java's java.util.Random class.
We will examine step by step the effect of the seed value on randomness, which classes should be preferred in multi-threaded environments, and the stream (Stream) methods that come with Java 8.
🎲 1. What is Random Class?
java.util.Random generates pseudorandom numbers using a specific algorithm.
These numbers, unlike true randomness, can be reproduced with the same seed value.
Features:
- By default, the system's nano time is used as the seed.
- Two Random instances started with the same seed produce the same sequence of numbers.
- Not secure, so should not be used in encryption or session keys.
ThreadLocalRandomfor multi-threading,SecureRandomrecommended for security.
⚙️ 2. Random Class Constructors
| Founder | Description |
|---|---|
Random() | Generates a random number with a default seed value. |
Random(long seed) | Generates deterministic (repeatable) random numbers with the specified seed value. |
🔢 3. Frequently Used Random Methods
| Method | Return Value | Description |
|---|---|---|
nextBoolean() | boolean | Generates true/false randomly. |
nextDouble() | double | It returns a random double between 0.0 and 1.0. |
nextFloat() | float | It returns a float value between 0.0 and 1.0. |
nextInt() | int | Generates a random integer. |
nextInt(int n) | int | Returns an integer between 0 (inclusive) and n (excluding). |
💻 4. Basic Use Case
The following example shows the most common methods of the Random class:
import java.util.Random;
public class RandomNumberExample {
public static void main(String[] args) {
Random rastgele = new Random();
System.out.println(rastgele.nextBoolean());
System.out.println(rastgele.nextDouble());
System.out.println(rastgele.nextInt());
System.out.println(rastgele.nextInt(20));
}
}
💡 Description: Each run produces different results because the default seed changes with system time.
🌱 5. Seed Usage
The seed value determines the starting point of the algorithm. Two Random objects initialized with the same seed value produce the same array:
import java.util.Random;
public class RandomSeedExample {
public static void main(String[] args) {
Random r1 = new Random(500);
Random r2 = new Random(500);
System.out.println(r1.nextInt());
System.out.println(r2.nextInt());
}
}
💬 Output:
-1193959466
-1193959466
Same core → same results ✅
⚡ 6. Stream-Based Randomness with Java 8
Java 8 added stream support to the Random class. In this way, multiple random values can be generated within a certain range:
import java.util.Random;
import java.util.stream.IntStream;
public class RandomStreamExample {
public static void main(String[] args) {
Random rastgele = new Random();
IntStream sayilar = rastgele.ints(5, 1, 100);
sayilar.forEach(System.out::println);
}
}
💡 Description: This code generates 5 random integers between 1 and 100.
🧭 7. Alternatives
| Class | Intended Use |
|---|---|
ThreadLocalRandom | Provides high performance in multi-threaded applications. |
SecureRandom | Recommended for encryption and security-oriented randomness generation. |
💡 Frequently Asked Questions (FAQ)
- What does “pseudo-random” mean?
Numbers are generated algorithmically; The same result can be obtained with the same seed value.
- Why is the seed important?
Seed determines the starting point of the generated sequence. Provides repeatable results in simulations.
- What does nextInt(10) produce?
Returns a value in the range 0–9 (no upper limit included).
- Is Random safe?
No, SecureRandom should be used for cryptographic purposes.
- Is it thread-safe?
Not exactly. ThreadLocalRandom is preferred in multi-thread structures.
🚀 Conclusion
java.util.Random is the basic tool for simple random number generation in Java. However, it is beneficial when used correctly; When used incorrectly, it can produce repeatable results.
Choose the most appropriate solution by correctly evaluating the difference between Random, ThreadLocalRandom or SecureRandom in your developments. You can try these examples on the virtual servers (instances) you create on the GenixNode platform and observe the performance differences.

