Why does this code using random strings print "hello world"? Why does this code using random strings print "hello world"? java java

Why does this code using random strings print "hello world"?


The other answers explain why, but here is how.

Given an instance of Random:

Random r = new Random(-229985452)

The first 6 numbers that r.nextInt(27) generates are:

851212150

and the first 6 numbers that r.nextInt(27) generates given Random r = new Random(-147909649) are:

2315181240

Then just add those numbers to the integer representation of the character ` (which is 96):

8  + 96 = 104 --> h5  + 96 = 101 --> e12 + 96 = 108 --> l12 + 96 = 108 --> l15 + 96 = 111 --> o23 + 96 = 119 --> w15 + 96 = 111 --> o18 + 96 = 114 --> r12 + 96 = 108 --> l4  + 96 = 100 --> d


When an instance of java.util.Random is constructed with a specific seed parameter (in this case -229985452 or -147909649), it follows the random number generation algorithm beginning with that seed value.

Every Random constructed with the same seed will generate the same pattern of numbers every time.


I'll just leave it here. Whoever has a lot of (CPU) time to spare, feel free to experiment :) Also, if you have mastered some fork-join-fu to make this thing burn all CPU cores (just threads are boring, right?), please share your code. I would greatly appreciate it.

public static void main(String[] args) {    long time = System.currentTimeMillis();    generate("stack");    generate("over");    generate("flow");    generate("rulez");    System.out.println("Took " + (System.currentTimeMillis() - time) + " ms");}private static void generate(String goal) {    long[] seed = generateSeed(goal, Long.MIN_VALUE, Long.MAX_VALUE);    System.out.println(seed[0]);    System.out.println(randomString(seed[0], (char) seed[1]));}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) {            int base = input[0] - pool[0];            for (int i = 1; i < input.length; i++) {                if (input[i] - pool[i] != base)                    continue label;            }            return new long[]{seed, base};        }    }    throw new NoSuchElementException("Sorry :/");}public static String randomString(long i, char base) {    System.out.println("Using base: '" + base + "'");    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) (base + k));    }    return sb.toString();}

Output:

-9223372036808280701Using base: 'Z'stack-9223372036853943469Using base: 'b'over-9223372036852834412Using base: 'e'flow-9223372036838149518Using base: 'd'rulezTook 7087 ms