1

I have see this code:

public class HelloWorld {

    public static void main(String ... args)           
    {
        System.out.println(randomString(-229985452)+''+randomString(-147909649));
    }

    public static String randomString(int seed) 
    {
        Random rand = new Random(seed);
        StringBuilder sb = new StringBuilder();
        while(true) {
            int n = rand.nextInt(27);
            if (n == 0) break;
            sb.append((char) ('`' + n));
        }
        return sb.toString();
    }
}

and the work result is : hello world

I want to know why is it

VC.One
  • 14,790
  • 4
  • 25
  • 57
Timi
  • 892
  • 1
  • 8
  • 17

1 Answers1

0

Random numbers are not really random, but pseudo random. You can search through the pseudo-random path that they follow, looking for small patterns.

In this case, the pattern is 'hello world'.

If you started at a different seed, then you would get nonsense; but, since you started at a seed that is known to walk the next few characters of hello world, you'll get hello world.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138