生成随机数列

来源:互联网 发布:淘宝网开网店要钱吗 编辑:程序博客网 时间:2024/06/07 01:50
public int[] randomIntList(int length,int max) throws Exception{  
        if(length > max){  
            throw new Exception("length必须小于等于max");  
        }  
        int[] r = new int[length];  
        ArrayList<Integer> t = new ArrayList<Integer>();  
        for(int i = 0 ; i < max ; i ++){  
            t.add(i);  
        }  
        int i = 0 ;  
        while(i < length){  
            int tr = new Random().nextInt(t.size());  
            r[i] = t.get(tr);  
            t.remove(tr);  
            i++;  
        }  
        return r;  
    }