Fundamentals 11 min read

Why Does a Random‑Seeded Java Method Print “Hello World”?

This article explains how using fixed seeds with Java's Random class generates deterministic pseudo‑random strings that can be crafted to output "hello world", demonstrates the underlying seed mechanics, shows how to reverse‑engineer seeds for arbitrary phrases, and discusses related quirks in the Java source code.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Why Does a Random‑Seeded Java Method Print “Hello World”?

Why a Random‑Seeded Method Prints Hello World

On StackOverflow a puzzling Java snippet prints "hello world" despite using seemingly random strings. The trick lies in the Random constructor receiving a fixed seed, which makes the generated number sequence deterministic.

Understanding the Seed

When new Random(seed) is called, the same seed (e.g., -229985452 or -147909649) produces the same sequence of numbers. Two Random instances with identical seeds and identical method call sequences return identical pseudo‑random numbers, as documented in the Javadoc.

How the Code Works

public class MainTest {
    public static void main(String[] args) {
        System.out.println(randomString(-229985452) + " " + randomString(-147909649));
    }
    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();
    }
}

The loop appends characters derived from ('`' + k), where k ranges from 1 to 26, mapping to ASCII letters a‑z. With the fixed seeds, the first five nextInt(27) values translate to the letters forming "hello" and "world".

Reversing the Process

By iterating over possible seeds, one can find a seed that generates any desired word. The article provides a utility method:

public static long generateSeed(String goal, long start, long finish) {
    char[] input = goal.toCharArray();
    char[] pool = new char[input.length];
    label:
    for (long seed = start; seed < finish; seed++) {
        Random random = new Random(seed);
        for (int i = 0; i < input.length; i++)
            pool[i] = (char) (random.nextInt(27) + '`');
        if (random.nextInt(27) == 0) {
            for (int i = 0; i < input.length; i++)
                if (input[i] != pool[i])
                    continue label;
            return seed;
        }
    }
    throw new NoSuchElementException("Sorry :/");
}

This brute‑force search can eventually find a seed for any phrase, though longer strings require exponentially more time.

Random Source Quirks

The article also uncovers a historical typo in Java's Random implementation: two magic numbers were missing a leading "1" due to a copy‑paste error, affecting the seed generation algorithm. The bug was later fixed in newer JDK versions.

Modern Alternatives

Today, developers rarely use java.util.Random directly; ThreadLocalRandom or cryptographic generators are preferred for better randomness.

Overall, the piece demonstrates how deterministic pseudo‑randomness can be exploited to produce specific outputs and highlights subtle implementation details in Java's random number generation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javacode analysisrandomHello Worldpseudo-randomseed
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.