Question
In Java, the following code prints hello world:
System.out.println(randomString(-229985452) + " " + randomString(-147909649));
The randomString() method is:
public static String randomString(int i) {
Random ran = new Random(i);
StringBuilder sb = new StringBuilder();
while (true) {
int k = ran.nextInt(27);
if (k == 0)
break;
sb.append((char) ('`' + k));
}
return sb.toString();
}
Why does this code generate the exact output hello world? How can seemingly random seeds produce a meaningful phrase?
Short Answer
By the end of this page, you will understand that Java's Random is not truly random. It is pseudorandom, which means the same seed always produces the same sequence of values. This code works because the chosen seeds were carefully selected so that the generated character sequence becomes hello and world. You will also learn how seeding works, how nextInt() drives the output, and why this is predictable rather than magical.
Concept
Java's Random class generates pseudorandom numbers. That means the numbers look random, but they are actually produced by a deterministic algorithm.
The key idea
When you write:
Random ran = new Random(i);
the value i becomes the seed. A seed is the starting point for the random number generator.
If you use the same seed again, Java's Random will generate the same sequence of numbers again.
So this method is not really creating arbitrary strings. It is creating a string based on a predictable number sequence.
What this method does
int k = ran.nextInt(27);
This returns a number from 0 to 26.
0means stop building the string1to26map to letters:
Mental Model
Think of Random like a music player with a fixed playlist.
It may sound random, but if you always start from the same track number, you will hear the same songs in the same order.
- The seed is the starting track.
nextInt(27)asks for the next item in the playlist.- Each number becomes a letter.
- When the number
0appears, the song ends.
So the code is not discovering hello world by chance. Someone picked starting points that make the playlist spell those words.
Syntax and Examples
Core syntax
Creating a seeded random generator in Java:
Random random = new Random(123);
int n = random.nextInt(10); // 0 to 9
Generating repeatable output:
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random a = new Random(42);
Random b = new Random(42);
System.out.println(a.nextInt(100));
System.out.println(b.nextInt(100));
}
}
Both lines print the same value because both generators use the same seed.
Beginner-friendly example
Step by Step Execution
Traceable example
Consider this simplified method:
public static String randomString(int seed) {
Random ran = new Random(seed);
StringBuilder sb = new StringBuilder();
while (true) {
int k = ran.nextInt(27);
if (k == 0) {
break;
}
sb.append((char) ('`' + k));
}
return sb.toString();
}
Now imagine the generator returns this sequence for a specific seed:
8, 5, 12, 12, 15, 0
Step by step
-
Create
Random ran = new Random(seed)- The seed fixes the number sequence.
-
Create an empty
StringBuilder
Real World Use Cases
Where seeded randomness is useful
Repeatable tests
Developers use fixed seeds so test results stay consistent:
Random random = new Random(12345);
This helps reproduce bugs.
Games and procedural generation
A game may generate the same map from the same seed.
- same seed -> same world
- different seed -> different world
Simulations
Scientific or financial simulations often need results that can be rerun exactly.
Debugging
If a bug happens with seed 9876, the developer can rerun the exact same sequence.
Demo data generation
You can generate fake but predictable names, IDs, or values for development.
Where this exact string-building idea appears
- creating random-looking usernames
- generating sample words for tests
- encoding hidden messages or puzzles
- challenge problems that demonstrate deterministic randomness
Real Codebase Usage
In real projects, developers usually do not use Random to hide words in code, but they do use the same underlying idea: fixed seeds create repeatable behavior.
Common patterns
Reproducible test setup
Random random = new Random(100);
Used in unit tests to generate the same sample data every run.
Dependency injection of randomness
Instead of creating new Random() inside a method, developers often pass a Random object in.
public String generateCode(Random random) {
// use random here
}
This makes the code easier to test.
Guarding against infinite loops
The original code depends on eventually getting 0. In production code, developers often add limits.
for (int count = 0; count < ; count++) {
random.nextInt();
(k == ) ;
}
Common Mistakes
1. Thinking Random is truly random
Beginners often assume this:
Random r = new Random(5);
means the result will be unpredictable. It will not. The same seed always gives the same sequence.
Avoid it
Use a fixed seed only when you want repeatable output.
2. Creating a new Random every time without understanding the effect
Broken idea:
public int getNumber() {
Random r = new Random(123);
return r.nextInt(10);
}
This returns the same first value every call.
Better
Random r = new Random(123);
{
r.nextInt();
}
Comparisons
Seeded vs unseeded randomness
| Approach | Behavior | Repeatable? | Common use |
|---|---|---|---|
new Random(123) | Same sequence every run | Yes | Testing, debugging, reproducible generation |
new Random() | Seed based on current state/time | Usually no | General-purpose randomness |
Pseudorandom vs true random
| Type | Meaning | Predictable from seed? | Typical source |
|---|---|---|---|
| Pseudorandom | Generated by algorithm | Yes | Random, software PRNGs |
Cheat Sheet
Quick reference
Seeded random
Random r = new Random(123);
- Same seed -> same sequence
- Different seed -> different sequence
nextInt(bound)
r.nextInt(27);
Returns an int from:
0 to 26
Character mapping in the original code
(char) ('`' + k)
1 -> a2 -> b...26 -> z0 -> stop
Original logic
FAQ
Why does the same seed always give the same output in Java?
Because Random uses a deterministic algorithm. The seed defines the starting state, and the same starting state produces the same sequence.
Is Java Random actually random?
No. It is pseudorandom. The values appear random, but they are generated by a predictable process.
Why does nextInt(27) allow the loop to stop?
Because it can return 0 through 26. The code uses 0 as a signal to break out of the loop.
Why is '`' used instead of 'a'?
Because '`' is the character before 'a'. That makes 1 map to 'a', 2 to 'b', and so on.
Were those seed values discovered by chance?
Most likely not. They were almost certainly chosen specifically so the generated sequences spell hello and world.
Mini Project
Description
Build a small Java program that demonstrates how seeded randomness creates repeatable words. The program should generate a word from a seed, print the same word for the same seed, and show different output for a different seed. This helps reinforce that Random is deterministic, not magical.
Goal
Create a Java program that turns seeded pseudorandom numbers into lowercase words and proves that the same seed always produces the same result.
Requirements
[ "Write a method that accepts an integer seed and returns a generated word.", "Use Random with the provided seed.", "Convert numbers into lowercase letters.", "Stop after a fixed number of letters or when a stop condition is reached.", "Show that the same seed produces the same word twice." ]
Keep learning
Related questions
Avoiding Java Code in JSP with JSP 2: EL and JSTL Explained
Learn how to avoid Java scriptlets in JSP 2 using Expression Language and JSTL, with examples, best practices, and common mistakes.
Choosing a @NotNull Annotation in Java: Validation vs Static Analysis
Learn how Java @NotNull annotations differ, when to use each one, and how to choose between validation, IDE hints, and static analysis tools.
Convert a Java Stack Trace to a String
Learn how to convert a Java exception stack trace to a string using StringWriter and PrintWriter, with examples and common mistakes.