Java伪随机数打印任意的字母数字

有这么一段代码,虽然看上去是使用Random类,但可以发现不管怎么运行,结果都是一样的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public static void main(String[] args) {
System.out.print(print(-9223372036854763965L));
System.out.print(print(-9223372036854760861L));
System.out.print(print(-9223372036854771289L));
System.out.print(print(-9223372036854774560L));
System.out.print(print(-9223372036854706559L));
System.out.print(print(-9223372036854774560L));
System.out.print(print(-9223372036854771289L));
System.out.print(print(-9223372036854748009L));
System.out.print(print(-9223372036854770262L));
System.out.print(print(-9223372036854706559L));
System.out.print(print(-9223372036854753589L));
System.out.print(print(-9223372036854771289L));
System.out.print(print(-9223372036854768597L));
}

public static String print(long i) {
Random ran = new Random(i);
StringBuilder sb = new StringBuilder();
while (true) {
int k = ran.nextInt(127);
if (k == 0) {
break;
}
sb.append((char) (k));
}
return sb.toString();
}

为什么会出现这种情况?

可以看到,上边创建Random实例时使用的是下边这个有参构造,平时我们使用的都是Random的无参构造,其实无参构造中也是使用的这个构造方法,只是默认给了个参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 有参构造
public Random(long seed) {
if (getClass() == Random.class)
this.seed = new AtomicLong(initialScramble(seed));
else {
// subclass might have overriden setSeed
this.seed = new AtomicLong();
setSeed(seed);
}
}

// 无参构造
public Random() {
this(seedUniquifier() ^ System.nanoTime());
}

引用参考资料中的一句话

计算机只能产生伪随机数而不能产生绝对随机的随机数,伪随机数并不是假随机数,这里的“伪”是有规律的意思,即计算机产生的伪随机数既是随机的又是有规律的。

只要给定了Random类固定的种子(即有参构造的seed参数),那么生成的随机数就是固定的。

如何像上边那样找到某个字母的Long值?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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(127));

if (random.nextInt(127) == 0) {
for (int i = 0; i < input.length; i++) {
if (input[i] != pool[i])
continue label;
}
return seed;
}
}
throw new NoSuchElementException("Sorry :/");
}

上边的代码,直接使用即可,比如获取v的值,System.out.println(generateSeed("v", Long.MIN_VALUE, Long.MAX_VALUE));输出得到-9223372036854771666,使用pring方法打印即是v,想获取某个单词的Long值也可以,只不过会耗时很长,因为上边方法原理是生成一个个字母数组来比对,直到找到这个字母为止。
所以,理论上,只要时间够长,就可以找到。

参考资料

1. 初看一脸懵逼,看懂直接跪下!
2. https://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world
3. 最透彻的关于“随机数种子”和“伪随机数”的产生原理