Displaying Hello World unconventionally by using a Random Number Generator

A very good friend of mine, with whom I enjoy discussing coding techniques, brought to my attention unfamiliar ways of displaying the infamous “Hello World”. Displaying “hello world” is usually the first program anyone tries in a new language.

The code below shows how we can display the said string by the use of a random number generator.

 

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();
}

 

How does it work? Well let’s see your best guess. Shouldn’t be that hard.

Original post:

http://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world

 

Leave a Reply