这是自己做着玩的一个小东西,出了这么个小问题。
怀疑是String指针的关系,也有可能是static的关系,还没搞明白。
另外有什么建议的话也请提出来,谢谢!!!

解决方案 »

  1.   

    MAIN方法中循环5次生成随机数,但是在控制台中输出的结果都一样
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^没有细看你的代码,根据你的问题只需将//随机器
    Random random = new Random();改为类成员(不是放在方法内)
    private static Random random = new Random();即可
    信誉值低,如果问题解决,请结贴。
      

  2.   

    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++一样随机数据的产生和种子有关,种子一样,随机数也就一样,看是不是这个问题? 
      

  3.   

    想法基本和 ntzls(三星堆)一致,关于你的信誉分减少的问题,可以参考“系统功能”中的“使用帮助”,里面有信誉分的问题!
      

  4.   

    我包行,原方法在同一milliseconds内调用产生的随机数相同,因为种子milliseconds相同。详见圣经《计算机程序设计艺术》。
      

  5.   

    你如果写成
    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实例化中,每次的结果都会不同。
    没有细看你的全部程序。希望这些小提示可以对你有帮助。
      

  6.   

    深入讨论一下:
    正如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实例来产生,其种子一样,但结果不一样,岂不是又违背了刚才关于种子的论断?
    希望大家继续给予解释:)
      

  7.   

    种子指的是初始种子,其实计算机中的随机算法都是伪随机的可能就是这么一个函数int rand(){
       static int seed;
       seed = (seed * 124);
       return seed;
    }
      

  8.   

    种子虽相同但每次调用next()产生的随机数不同(64位的二进制码相同的概率极小)。方法内直接
    //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?
      

  9.   

    大致是明白了!!!
    就是说我一开始的情况就是产生了5次循环使用了相同种子的Random做了相同的事,所以产生了5组相同的数字。
    但是我每次都重新实例化了一个新的Random,为什么它们的种子都相同呢?不好意思,我有点刨根问底了,这个问题让我对Random有了全新的认识,要感谢大家!
      

  10.   

    但是我每次都重新实例化了一个新的Random,为什么它们的种子都相同呢?默认种子是系统时钟milliseconds,短时间同时调用milliseconds相同。机器速度何其快。
      

  11.   

    这下是完全明白了,非常感谢各位的解答!
    特别是 ntzls(三星堆),这么晚了还在帮助我:)