2种取不重复随机数

public void test(){
List<Integer> list = new ArrayList<Integer>();  
       
        for(int i = 1; i <= 6; i++){  
            list.add(i);  
        }  
 
        Collections.shuffle(list);  
          
        for(Integer i : list){  
            System.out.println(i);  
        }  
}

public void test_2(){
int a[] = new int[6];  
    for (int i = 0; i < a.length; i++) {  
      a[i] = (int) (Math.random() * 6) + 1;  
      for (int k = 0; k < i; k++) {  
        if (a[i] == a[k]) {  
          i--;  
          continue;  
        }  
      }  
    }  
    System.out.println(Arrays.toString(a));  

}

你可能感兴趣的:(随机数)