1. 程式人生 > >java得到指定位數隨機密碼(由數字,區分大小寫的字母組成)

java得到指定位數隨機密碼(由數字,區分大小寫的字母組成)

    /**
    *獲取指定位數密碼
    */
    public String getCode(int length){
        char[] m = new char[length];
        for(int i = 0;i < length;i++){
            m[i] = get();
        }
        return new String(m);
    }

    private char get(){
        int begin = 0;
        int end = 0;
        switch(new
Random().nextInt(3)){ case 0: begin = 10; end = 48; break; case 1: begin = 26; end = 65; break; case 2: begin = 26; end = 97; break; } return (char) (new
Random().nextInt(begin) + end); }