MAIN方法中循环5次生成随机数,但是在控制台中输出的结果都一样 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^没有细看你的代码,根据你的问题只需将//随机器 Random random = new Random();改为类成员(不是放在方法内) private static Random random = new Random();即可 信誉值低,如果问题解决,请结贴。
An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods. The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits. Many applications will find the random method in class Math simpler to use.和C++一样随机数据的产生和种子有关,种子一样,随机数也就一样,看是不是这个问题?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^没有细看你的代码,根据你的问题只需将//随机器
Random random = new Random();改为类成员(不是放在方法内)
private static Random random = new Random();即可
信誉值低,如果问题解决,请结贴。
If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods. The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits. Many applications will find the random method in class Math simpler to use.和C++一样随机数据的产生和种子有关,种子一样,随机数也就一样,看是不是这个问题?
for(int i=0;i<10;i++){
Random _rd=new Random();
System.out.println(_rd.nextInt());
}
应该会发现基本上在控制台打印出来的随机数都是一样的。
实例化一个Random对象的时候会根据cpu clock cycle来生成一个伪随机数,这样循环十次用不了一豪秒,所以生成的随机数相同。如果:
for(int i=0;i<10;i++){
Random _rd=new Random(i);
System.out.println(_rd.nextInt());
}
即把变量i作为种子放在Random实例化中,每次的结果都会不同。
没有细看你的全部程序。希望这些小提示可以对你有帮助。
正如anotherleeo(听风看雨) 所说的,使用不同的种子会得到不同的结果。另外 ntzls(三星堆)的做法我也成功了,代码如下:
//随机器
private static Random random = new Random();public static ArrayList getNumberList(int from,int to,int amount,boolean repeatable) {
//随机器
Random random = NumberCreator.random;
......
}
这样的结果也不一样,但是这一点我没有想明白,既然所有的随机数都是由此唯一的Random实例来产生,其种子一样,但结果不一样,岂不是又违背了刚才关于种子的论断?
希望大家继续给予解释:)
static int seed;
seed = (seed * 124);
return seed;
}
//Random random = NumberCreator.random;省略
number=random.nextInt(to+1);即可不建议用anotherleeo(听风看雨) 方法改种子。Random random1 = new Random(123456l);//相同的种子
Random random2 = new Random(123456l);
random1.nextInt() == random2.nextInt();//均是第一个随机数因而相等。
random1.nextInt();
random1.nextInt() != random2.nextInt();//random1的第三个随机数与2的第二个随机数不同可见相同种子的random第n个随机数相等。OK?
就是说我一开始的情况就是产生了5次循环使用了相同种子的Random做了相同的事,所以产生了5组相同的数字。
但是我每次都重新实例化了一个新的Random,为什么它们的种子都相同呢?不好意思,我有点刨根问底了,这个问题让我对Random有了全新的认识,要感谢大家!
特别是 ntzls(三星堆),这么晚了还在帮助我:)