生成8位隨機大小寫字母加數字隨機數


public static String getRundom(){
//         48-57 65-90 97-122
        StringBuffer id=new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < 8; i++) {
            char s = 0;
            int j=random.nextInt(3) % 4;
            switch (j) {
            case 0:
                //隨機生成數字
                s = (char) (random.nextInt(57) % (57 - 48 + 1) + 48);
                break;
            case 1:
                //隨機生成大寫字母
                s = (char) (random.nextInt(90) % (90 - 65 + 1) + 65);            
                break;
            case 2:
                //隨機生成小寫字母
                s = (char) (random.nextInt(122) % (122 - 97 + 1) + 97);
                break;
            }
            id.append(s);
        }
        return id.toString();
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM