为什么这段代码使用随机字符串打印"hello world"?

The following print statement would print "hello world". Could anyone explain this?

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

And randomString() looks like this:

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

转载于:https://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world


不负相思意
浏览 468回答 4
4回答

HUWWW

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.

弑天下

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:8 5 12 12 15 0and the first 6 numbers that r.nextInt(27) generates given Random r = new Random(-147909649) are:23 15 18 12 4 0Then just add those numbers to the integer representation of the character ` (which is 96):8  + 96 = 104 --> h 5  + 96 = 101 --> e 12 + 96 = 108 --> l 12 + 96 = 108 --> l 15 + 96 = 111 --> o 23 + 96 = 119 --> w 15 + 96 = 111 --> o 18 + 96 = 114 --> r 12 + 96 = 108 --> l 4  + 96 = 100 --> d

翻过高山走不出你

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&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;time&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;generate("stack"); &nbsp;&nbsp;&nbsp;&nbsp;generate("over"); &nbsp;&nbsp;&nbsp;&nbsp;generate("flow"); &nbsp;&nbsp;&nbsp;&nbsp;generate("rulez"); &nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Took&nbsp;"&nbsp;+&nbsp;(System.currentTimeMillis()&nbsp;-&nbsp;time)&nbsp;+&nbsp;"&nbsp;ms"); } private&nbsp;static&nbsp;void&nbsp;generate(String&nbsp;goal)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;long[]&nbsp;seed&nbsp;=&nbsp;generateSeed(goal,&nbsp;Long.MIN_VALUE,&nbsp;Long.MAX_VALUE); &nbsp;&nbsp;&nbsp;&nbsp;System.out.println(seed[0]); &nbsp;&nbsp;&nbsp;&nbsp;System.out.println(randomString(seed[0],&nbsp;(char)&nbsp;seed[1])); } public&nbsp;static&nbsp;long[]&nbsp;generateSeed(String&nbsp;goal,&nbsp;long&nbsp;start,&nbsp;long&nbsp;finish)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;char[]&nbsp;input&nbsp;=&nbsp;goal.toCharArray(); &nbsp;&nbsp;&nbsp;&nbsp;char[]&nbsp;pool&nbsp;=&nbsp;new&nbsp;char[input.length]; &nbsp;&nbsp;&nbsp;&nbsp;label: &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(long&nbsp;seed&nbsp;=&nbsp;start;&nbsp;seed&nbsp;<&nbsp;finish;&nbsp;seed++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Random&nbsp;random&nbsp;=&nbsp;new&nbsp;Random(seed); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;input.length;&nbsp;i++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pool[i]&nbsp;=&nbsp;(char)&nbsp;random.nextInt(27); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(random.nextInt(27)&nbsp;==&nbsp;0)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;base&nbsp;=&nbsp;input[0]&nbsp;-&nbsp;pool[0]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;1;&nbsp;i&nbsp;<&nbsp;input.length;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(input[i]&nbsp;-&nbsp;pool[i]&nbsp;!=&nbsp;base) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue&nbsp;label; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;long[]{seed,&nbsp;base}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;NoSuchElementException("Sorry&nbsp;:/"); } public&nbsp;static&nbsp;String&nbsp;randomString(long&nbsp;i,&nbsp;char&nbsp;base)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Using&nbsp;base:&nbsp;'"&nbsp;+&nbsp;base&nbsp;+&nbsp;"'"); &nbsp;&nbsp;&nbsp;&nbsp;Random&nbsp;ran&nbsp;=&nbsp;new&nbsp;Random(i); &nbsp;&nbsp;&nbsp;&nbsp;StringBuilder&nbsp;sb&nbsp;=&nbsp;new&nbsp;StringBuilder(); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;n&nbsp;=&nbsp;0;&nbsp;;&nbsp;n++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;k&nbsp;=&nbsp;ran.nextInt(27); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(k&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sb.append((char)&nbsp;(base&nbsp;+&nbsp;k)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;sb.toString(); }Output:-9223372036808280701 Using&nbsp;base:&nbsp;'Z' stack -9223372036853943469 Using&nbsp;base:&nbsp;'b' over -9223372036852834412 Using&nbsp;base:&nbsp;'e' flow -9223372036838149518 Using&nbsp;base:&nbsp;'d' rulez Took&nbsp;7087&nbsp;ms
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java