java 隨機獲取Mac地址

public String getMac() {
				StringBuffer mac = new StringBuffer();
				for(int i=1; i<=6; i++) {
					String one = getStringRandom(1);
					String two = getStringRandom(1);
					mac.append(one).append(two);
					if(i != 6) mac.append(":");
				}
				return mac.toString().toLowerCase();
			}
			//生成隨機數字和字母,  
		    public String getStringRandom(int length) {  
		        String val = "";
		        Random random = new Random();
		        //參數length,表示生成幾位隨機數 
		        for(int i = 0; i < length; i++) {
		            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";  
		            //輸出字母仍是數字 
		            if( "char".equalsIgnoreCase(charOrNum) ) {
		                //輸出是大寫字母仍是小寫字母 
		                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
		                val += (char)(random.nextInt(6) + temp);
		            } else if( "num".equalsIgnoreCase(charOrNum) ) {
		                val += String.valueOf(random.nextInt(10)); 
		            }
		        }
		        return val;  
		    }
相關文章
相關標籤/搜索