for(int i=1;i<=5;i++){
               one = (int) (rd.nextDouble() * 32);
               while(one==0)
               two = (int) (rd.nextDouble() * 32);
               while(one==two||two==0){
                   two = (int) (rd.nextDouble() * 32);
               }
               three = (int) (rd.nextDouble() * 32);
               while(one==three||two==three||three==0){
                   three = (int) (rd.nextDouble() * 32);
               }
               four = (int) (rd.nextDouble() * 32);
               while(one==four||two==four||three==four||four==0){
                   four = (int) (rd.nextDouble() * 32);
               }
               five = (int) (rd.nextDouble() * 32);
               while(one==five||two==five||three==five||four==five||five==0){
                   five = (int) (rd.nextDouble() * 32);
               }
               six = (int) (rd.nextDouble() * 32);
               while(one==six||two==six||three==six||four==six||four==six||six==0){
                   six = (int) (rd.nextDouble() * 32);
               }
               seven = (int) (rd.nextDouble() * 16);
               while(seven==0){
                   seven = (int) (rd.nextDouble() * 16);
               }
               Vector ve=new Vector();
               ve.add(i);
               ve.add(one);
               ve.add(two);
               ve.add(three);
               ve.add(four);
               ve.add(five);
               ve.add(six);
               ve.add(seven);
               cp.dtm.addRow(ve);
           }
           for(int i=1;i<=5;i++){
               one = (int) (rd.nextDouble() * 32);
               while(one==0)
               two = (int) (rd.nextDouble() * 32);
               while(one==two||two==0){
                   two = (int) (rd.nextDouble() * 32);
               }
               three = (int) (rd.nextDouble() * 32);
               while(one==three||two==three||three==0){
                   three = (int) (rd.nextDouble() * 32);
               }
               four = (int) (rd.nextDouble() * 32);
               while(one==four||two==four||three==four||four==0){
                   four = (int) (rd.nextDouble() * 32);
               }
               five = (int) (rd.nextDouble() * 32);
               while(one==five||two==five||three==five||four==five||five==0){
                   five = (int) (rd.nextDouble() * 32);
               }
               six = (int) (rd.nextDouble() * 32);
               while(one==six||two==six||three==six||four==six||four==six||six==0){
                   six = (int) (rd.nextDouble() * 32);
               }
           }
               Vector ve=new Vector();
               ve.add(i);
               ve.add(one);
               ve.add(two);
               ve.add(three);
               ve.add(four);
               ve.add(five);
               ve.add(six);
               ve.add(seven);
               cp.dtm.addRow(ve);
           }
 
以时间做随即种子,让6个随即数在1-32之间,并且6个数字不会出现重复,而且不为0
连续生成5组这样的数
结果出人意料,基本上99%几率,5组数中第2个数都一样
为什么会这样????????????????????????????????????????????

解决方案 »

  1.   

    说说我的理解把.希望对楼主有帮助.
    Random rd = new Random();和Random rd = new Random(System.currentTimeMillis());是一致的。
    就是说在一个时间点到下一个时间点之间种子是一样的,
    这样就只有一种随即序列,当然这样并不能说明楼主的情况。
    目的就是让楼主栽重构代码时,比如楼主要将生成随机数的代码抽出
    public int getRandomNumber() {
    Random rd = new Random();
    rd.nextInt();
    }
    这种写法是不妥的.因为在同一个时间段(两个毫秒之间)的调用会产生完全一致的数列.产生楼主问题的原因应该是java的随机数产生器并不是完全的产生器。
    就是说分布并不均匀。至于解决办法我想根据reference
    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html
    使用int nextInt(int n) 就可以了    public void testRandom() {
            int one = 0;
            int two = 0;
            int three = 0;
            int four = 0;
            int five = 0;
            int six = 0;
            for(int   i=1;i <=5;i++){ 
                one = getRandomNum();
                while(one==0) { 
                    one = getRandomNum();
                }
                
                two = getRandomNum();
                while(one==two || two==0){ 
                        two = getRandomNum();
                } 
                
                three = getRandomNum();
                while(one==three || two==three || three==0){ 
                        three = getRandomNum();
                } 
                
                four = getRandomNum();
                while(one==four || two==four || three==four || four==0){ 
                        four = getRandomNum();
                } 
                
                five = getRandomNum();
                while(one==five  || two==five || three==five || four==five  || five==0){ 
                        five = getRandomNum();
                }             six = getRandomNum();
                while(one==six || two==six  || three==six  || four==six  || four==six || six==0){ 
                        six = getRandomNum();
                }
                System.out.print("\r\n");
                System.out.print(" one:"+one);
                System.out.print(" two:"+two);
                System.out.print(" three:"+three);
                System.out.print(" four:"+four);
                System.out.print(" five:"+five);
                System.out.println(" six:"+six);
            }     }
        public int getRandomNum() {
            return rd.nextInt(32)+1;
        }
        final static Random rd = new Random();
      

  2.   


    one = (int)(rd.nextDouble() * 32); while(one==0){
      one = (int)(rd.nextDouble() * 32); 
    }two = (int)(rd.nextDouble() * 32); while(one==two¦¦two==0){ 
      two = (int)(rd.nextDouble() * 32); 
    } two那里可能漏掉了两句吧...
      

  3.   

    有那么复杂吗?我提供一个算法请参考
    import java.util.Random;
    import java.util.Set;
    import java.util.TreeSet;public class TestRandom {
      static Random ran;;  public static Set<Integer> getRandom() {
        Set<Integer> set = new TreeSet<Integer>();    while (set.size() < 5) {
          set.add(ran.nextInt(31) + 1);
        }
        return set;
      }  public static void main(String[] args) {
        ran = new Random(System.currentTimeMillis());
        for (int i = 0; i < 6; i++) {
          Set<Integer> set = getRandom();
          for (Integer id : set) {
            System.out.print(id + ",");
          }
          System.out.println();
        }
      }
    }注意,这个算法不能保证2组不重复。
      

  4.   

    本帖最后由 java2000_net 于 2008-02-14 11:04:51 编辑
      

  5.   

    我把Random  修饰成final  
    然后将原来的算法写到到这个类的另一个方法,然后调用方法
    把原来的6个int 接受,改成了一个int [] 数组,结果就不会重复了......奇怪!