下面的打印语句将打印“hello world”。有人能解释一下吗?

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

randomString()如下所示:

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

当前回答

这是关于“种子”的。相同的种子产生相同的结果。

其他回答

事实上,大多数随机数生成器都是“伪随机”的。它们是线性同余生成器,或LCG(http://en.wikipedia.org/wiki/Linear_congruential_generator)

给定固定的种子,LCG是非常可预测的。基本上,使用给你第一个字母的种子,然后编写一个应用程序,继续生成下一个int(char),直到你命中目标字符串中的下一个字母,并记下你需要调用LCG的次数。继续,直到生成每个字母。

当java.util.Random的实例使用特定的种子参数(在本例中为-22985452或-147090649)构建时,它遵循从该种子值开始的随机数生成算法。

使用相同种子构建的每个随机数每次都会生成相同的数字模式。

由于多线程在Java中非常容易,这里有一个变体,它使用所有可用的内核搜索种子:http://ideone.com/ROhmTA

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class SeedFinder {

  static class SearchTask implements Callable<Long> {

    private final char[] goal;
    private final long start, step;

    public SearchTask(final String goal, final long offset, final long step) {
      final char[] goalAsArray = goal.toCharArray();
      this.goal = new char[goalAsArray.length + 1];
      System.arraycopy(goalAsArray, 0, this.goal, 0, goalAsArray.length);
      this.start = Long.MIN_VALUE + offset;
      this.step = step;
    }

    @Override
    public Long call() throws Exception {
      final long LIMIT = Long.MAX_VALUE - this.step;
      final Random random = new Random();
      int position, rnd;
      long seed = this.start;

      while ((Thread.interrupted() == false) && (seed < LIMIT)) {
        random.setSeed(seed);
        position = 0;
        rnd = random.nextInt(27);
        while (((rnd == 0) && (this.goal[position] == 0))
                || ((char) ('`' + rnd) == this.goal[position])) {
          ++position;
          if (position == this.goal.length) {
            return seed;
          }
          rnd = random.nextInt(27);
        }
        seed += this.step;
      }

      throw new Exception("No match found");
    }
  }

  public static void main(String[] args) {
    final String GOAL = "hello".toLowerCase();
    final int NUM_CORES = Runtime.getRuntime().availableProcessors();

    final ArrayList<SearchTask> tasks = new ArrayList<>(NUM_CORES);
    for (int i = 0; i < NUM_CORES; ++i) {
      tasks.add(new SearchTask(GOAL, i, NUM_CORES));
    }

    final ExecutorService executor = Executors.newFixedThreadPool(NUM_CORES, new ThreadFactory() {

      @Override
      public Thread newThread(Runnable r) {
        final Thread result = new Thread(r);
        result.setPriority(Thread.MIN_PRIORITY); // make sure we do not block more important tasks
        result.setDaemon(false);
        return result;
      }
    });
    try {
      final Long result = executor.invokeAny(tasks);
      System.out.println("Seed for \"" + GOAL + "\" found: " + result);
    } catch (Exception ex) {
      System.err.println("Calculation failed: " + ex);
    } finally {
      executor.shutdownNow();
    }
  }
}

这是关于“种子”的。相同的种子产生相同的结果。

Random始终返回相同的序列。它用于重排数组和其他排列操作。

要获得不同的序列,需要在某个位置初始化序列,称为“种子”。

randomSting在“随机”序列的i位置(种子=-22985452)获得随机数。然后将ASCII码用于种子位置之后的序列中的下一个27个字符,直到该值等于0。这将返回“hello”。同样的操作也适用于“世界”。

我认为该代码不适用于任何其他单词。编程的人非常了解随机序列。

这是非常棒的极客代码!