我知道如何得到在0到任何数字之间的随机数的范围。
但是我想知道的是,因为随机数发生器不是真正的随机,并且遵循一个特定的算法,例如,如果你传递一个20的种子,那么它总是产生相同的数字序列: 17,292,0,9。
所以我明白了。由于它遵循特定的算法,有什么方法可以强迫生成器始终以零或任何其他数字开始吗?
但特别是我的案子是零的。
发布于 2018-11-22 11:51:00
public static void main (String[] args) throws java.lang.Exception
{
int x = -1;
long seed = 0;
int xxx = 100;
while(x!=0){
Random s = new Random(seed++);
x = s.nextInt(xxx);
}
System.out.println("seed " + (seed-1) + " gives " + new Random(seed-1).nextInt(xxx));
}这将找到一个种子,对于给定的模数,下一个int将为零。(这个例子恰好是18 )。
发布于 2018-11-22 12:05:18
不需要破解随机类,只需自己写:
public class RandomGenerator {
private int bound;
private Random random;
private boolean firstCall = true;
public RandomGenerator(int bound, long seed) {
this.bound = bound;
random = new Random(seed)
}
public int next() {
if (firstCall) {
firstCall = false;
return 0;
}
return random.nextInt(bound);
}
}https://stackoverflow.com/questions/53430141
复制相似问题