Subscribe via RSS Become a friend on Facebook Follow me on Twitter

Cymons Games

Randomly generated words

March 4th, 2013

On Stack Overflow a block of Java code was shown with a question about why it produces the output it does.

System.out.println(randomString(-229985452) + " " + randomString(-147909649));

public static String randomString(int i)

{
    Random ran = new Random(i);
    StringBuilder sb = new StringBuilder();
    for (int n = 0; ; n++)
    {
        int k = ran.nextInt(27);
        if (k == 0)
            break;

        sb.append((char)('`' + k));
    }

    return sb.toString();
}

The answer, of course, is that random in computer isn’t really random at all and can be controlled with the seed you put into it, so it’s important to use a seed that isn’t static to avoid predictability. However the bigger question, to me, is how did they find those seed values that created the output they wanted?

The answer to that question, shown further down the stack overflow page and again in C on reddit, is basically you try them all then search for the output you want. This is basically sticking a bunch of monkeys in a room with typewriters and grepping for Hamlet.

What’s the practical application for this stuff? Almost none. But it’s a great way to obfscuate your output so no one can see the secret message embedded in your code, which could be a good way to release spoiler free code in case want players to have incentive to play your game.

Leave a Reply

Cymons Games. All programs provided without guarantee or warranty. Maintained by Joseph Larson.
If you have any questions or notice something is wrong please contact me. Powered by WordPress.