英文:
How does Math.Random in Java create random numbers
问题
我可以帮您翻译以下内容:
"I was wandering how I can create my own random number generator for reverse engineering mc seeds by setting starting conditions such as time .thank you in advance."
我在想如何创建自己的随机数生成器,以便通过设置起始条件如时间来进行反向工程MC种子。提前谢谢。
英文:
I was wandering how I can create my own random number generator for reverse engineering mc seeds by setting starting conditions such as time .thank you in advance.
答案1
得分: 1
public double nextDouble() {
return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT;
}
DOUBLE_UNIT 是 private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53)。来源。
next
的实现如下:
protected int next(int bits) {
long oldseed, nextseed;
AtomicLong seed = this.seed;
do {
oldseed = seed.get();
nextseed = (oldseed * multiplier + addend) & mask;
} while (!seed.compareAndSet(oldseed, nextseed));
return (int)(nextseed >>> (48 - bits));
}
正如您所看到的,这是一个线性同余伪随机数生成器。
1: https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/Math.java#L809
2: https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/Random.java#L531
3: https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/Random.java#L92
4: https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/Random.java#L198
英文:
-
Math.random calls an object of type Random, Source
-
nextDouble
is called, Source -
nextDouble
is implemented like this:
public double nextDouble() {
return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT;
}
DOUBLE_UNIT is private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53) Source.
next
is implemented like this:
protected int next(int bits) {
long oldseed, nextseed;
AtomicLong seed = this.seed;
do {
oldseed = seed.get();
nextseed = (oldseed * multiplier + addend) & mask;
} while (!seed.compareAndSet(oldseed, nextseed));
return (int)(nextseed >>> (48 - bits));
}
As you can see, it's a
>linear congruential pseudorandom number generator
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论